Android布局——常用控件研究
2017-06-12 18:42
429 查看
TextView:
只读显示控件,可通过getText()获取其android:text属性、setText()设置其android:text属性。在res/layout/activity_main.xml的LinearLayout节中添加如下代码来声明TextView。
在java代码中可以通过下列代码取得该控件。
Button:
按钮控件,用户通过该控件来提交自己的请求,是用户与应用程序交互的最常用控件之一。
在res/layout/main.xml中声明控件
响应用户的动作
EditText:
用户输入的文本框
在res/layout/main.xml中添加EditText标签。
EditText回车动作,取得EditText的值以及如何为TextView赋值。
RadioButton:
单选按钮,放在一个RadioGroup中,在这个group中只能有一个选项能够被选中,比如你选择性别时,只能选择一个性别。
在layout/main.xml中声明控件。
为了响应RadioButton的选择动作,我们可以有两种方式。
A、定义单独的一个 myClickListener监听器,然后为每一个Radio注册这个监听器。
A方法是只要你点击单选按钮系统就会响应,而不管你点击的是否为同一个RadioButton;
B方法只会响应不同的单选动作,如果在同一个RadioButton上重复点击,系统是不会响应的。
CheckBox:
复选按钮。在应用程序中如果让用户选择自己的喜好时,因为用户的喜好不可能只有一个,所以这时你必须使用复选按钮,允许用户多选。
在res/layout/main.xml中声明控件。
响应用户的选择操作。
ToggleButton:
在开启和关闭WIFI的时候,有一个按钮,开启WIFI时按钮显示为On,关闭WIFI时按钮显示为OFF,这个按钮只有这两种状态。这个按钮就是ToggleButton。
在res/layout/main.xml中定义控件。
android:textOn——当在打开状态时,显示在这个按钮上的文字
android:textOff——当在关闭状态时,显示在这个按钮上的文字
响应Toggle Button的Checked事件
RatingButton:
评分条。可以根据我们的需要来定义他的增长速率和最大值。
在res/layout/main.xml中声明控件
android:numStarts——打分条中五角星的个数
android:stepSize——也就是你点击一次,会选中一个星星的多少,如果为1.0,那点击一次就是一颗星,如果设置为0.5那么就是半颗星。
只读显示控件,可通过getText()获取其android:text属性、setText()设置其android:text属性。在res/layout/activity_main.xml的LinearLayout节中添加如下代码来声明TextView。
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/myTextView" />
在java代码中可以通过下列代码取得该控件。
//取得该控件 TextView myTextView =(TextView)findViewById(R.id.myTextView);
Button:
按钮控件,用户通过该控件来提交自己的请求,是用户与应用程序交互的最常用控件之一。
在res/layout/main.xml中声明控件
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/myButton" />
响应用户的动作
//响应Button的Click事件 myButton = (Button) findViewById(R.id.myButton); myButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(Android_Control_DemoActivity.this, "click Button", Toast.LENGTH_SHORT).show(); } });
EditText:
用户输入的文本框
在res/layout/main.xml中添加EditText标签。
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/myEditText" />
EditText回车动作,取得EditText的值以及如何为TextView赋值。
//操作EditText控件,取值以及响应事件 myEditText = (EditText)findViewById(R.id.myEditText); myEditText.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { // 响应用户的回车键动作,将EditText中值显示到TextView中 if ((event.getAction() == KeyEvent.ACTION_DOWN) &&(keyCode == KeyEvent.KEYCODE_ENTER)) { myTextView.setText(myEditText.getText()); return true; } return false; } });
RadioButton:
单选按钮,放在一个RadioGroup中,在这个group中只能有一个选项能够被选中,比如你选择性别时,只能选择一个性别。
在layout/main.xml中声明控件。
<RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:text="Red" android:layout_height="wrap_content" android:checked="true"></RadioButton> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:text="Blue" android:layout_height="wrap_content"></RadioButton> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:text="Green" android:layout_height="wrap_content"></RadioButton> </RadioGroup>
为了响应RadioButton的选择动作,我们可以有两种方式。
A、定义单独的一个 myClickListener监听器,然后为每一个Radio注册这个监听器。
//1、定义一个我们自己的 myClickListener监听器,事件类型为OnClickListener OnClickListener myClickListener=new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub RadioButton r=(RadioButton)v; Toast.makeText(Android_Control_DemoActivity.this, r.getText(), Toast.LENGTH_SHORT).show(); } }; final RadioButton radio_1=(RadioButton)findViewById(R.id.radio0); final RadioButton radio_2=(RadioButton)findViewById(R.id.radio1); final RadioButton radio_3=(RadioButton)findViewById(R.id.radio2); radio_1.setOnClickListener(myClickListener); radio_2.setOnClickListener(myClickListener); radio_3.setOnClickListener(myClickListener); B、 为RadioGroup绑定事件,在这个事件中判断选择的按钮ID来响应事件。 //2、使用RadioGroup的CheckedChange方法来监听我们的RadioButton的点击事件 myRadioGroup =(RadioGroup)findViewById(R.id.radioGroup1); myRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(radio_1.getId()==checkedId) { Toast.makeText(Android_Control_DemoActivity.this, radio_1.getText(), Toast.LENGTH_SHORT).show(); } if (radio_2.getId()==checkedId) { Toast.makeText(Android_Control_DemoActivity.this, radio_2.getText(), Toast.LENGTH_SHORT).show(); } if(radio_3.getId()==checkedId) { Toast.makeText(Android_Control_DemoActivity.this, radio_3.getText(), Toast.LENGTH_SHORT).show(); } } });
A方法是只要你点击单选按钮系统就会响应,而不管你点击的是否为同一个RadioButton;
B方法只会响应不同的单选动作,如果在同一个RadioButton上重复点击,系统是不会响应的。
CheckBox:
复选按钮。在应用程序中如果让用户选择自己的喜好时,因为用户的喜好不可能只有一个,所以这时你必须使用复选按钮,允许用户多选。
在res/layout/main.xml中声明控件。
<CheckBox android:text="CheckBox" android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
响应用户的选择操作。
//为myChecBox控件绑定响应事件 myCheckBox = (CheckBox)findViewById(R.id.checkBox1); myCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { Toast.makeText(Android_Control_DemoActivity.this, myCheckBox.getText(), Toast.LENGTH_SHORT).show(); } } });
ToggleButton:
在开启和关闭WIFI的时候,有一个按钮,开启WIFI时按钮显示为On,关闭WIFI时按钮显示为OFF,这个按钮只有这两种状态。这个按钮就是ToggleButton。
在res/layout/main.xml中定义控件。
<ToggleButton android:text="ToggleButton" android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="On" android:textOff="Off"></ToggleButton>
android:textOn——当在打开状态时,显示在这个按钮上的文字
android:textOff——当在关闭状态时,显示在这个按钮上的文字
响应Toggle Button的Checked事件
//响应myToggleButton的Checked事件 myToggleButton = (ToggleButton)findViewById(R.id.toggleButton1); myToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { Toast.makeText(Android_Control_DemoActivity.this, myToggleButton.isChecked()+"", Toast.LENGTH_SHORT).show(); } } });
RatingButton:
评分条。可以根据我们的需要来定义他的增长速率和最大值。
在res/layout/main.xml中声明控件
<RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:stepSize="1.0"></RatingBar>
android:numStarts——打分条中五角星的个数
android:stepSize——也就是你点击一次,会选中一个星星的多少,如果为1.0,那点击一次就是一颗星,如果设置为0.5那么就是半颗星。
//响应nyRatingBar的RatingBarChange事件 myRatingBar =(RatingBar)findViewById(R.id.ratingBar1); myRatingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { // TODO Auto-generated method stub Toast.makeText(Android_Control_DemoActivity.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show(); } });
相关文章推荐
- 黑马程序员_Android-Activity布局初步(二)与常用控件(二)
- android编码规范 & 常用布局 & 常用控件
- Android 控件布局常用属性
- 10天学通Android开发(4)-用户布局与常用控件
- Android布局常用控件
- Android 控件布局常用属性
- Android 控件布局常用属性
- Android 控件布局常用属性
- Android相对布局中控件的常用属性【转】
- android相对布局中控件的常用属性
- android 控件位置常用布局
- android相对布局中控件的常用属性【转】
- Android相对布局中控件的常用属性【转】
- (3)Android入门——android编码规范 & 常用布局 & 常用控件
- 三,android编码规范 & 常用布局 & 常用控件
- Android 控件布局常用属性
- android编码规范 & 常用布局 & 常用控件
- Android 控件布局常用属性
- Android 控件布局常用属性
- Android 控件布局常用属性