android CheckBox控件的定义及事件监听
来源:互联网 发布:3d游戏美工 编辑:程序博客网 时间:2024/06/07 01:37
</pre><p>CheckBox控件的定义,main.xml内容如下:</p><p><pre name="code" class="html"> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请选择" /> <CheckBox android:id="@+id/beijing" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="北京" /> <CheckBox android:id="@+id/shanghai" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上海" /> <CheckBox android:id="@+id/shenzhen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="深圳" /> </LinearLayout>
activity CheckBoxTest.java内容如下:
package checkbox.pack; import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; public class CheckBoxTest extends Activity { //对控件对象进行声明 CheckBox beijing=null; CheckBox shanghai=null; CheckBox shenzhen=null; EditText editText1=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //通过控件的ID来得到代表控件的对象 beijing=(CheckBox)findViewById(R.id.beijing); shanghai=(CheckBox)findViewById(R.id.shanghai); shenzhen=(CheckBox)findViewById(R.id.shenzhen); editText1=(EditText)findViewById(R.id.editText1); //给CheckBox设置事件监听 beijing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ editText1.setText(buttonView.getText()+"选中"); }else{ editText1.setText(buttonView.getText()+"取消选中"); } } }); shanghai.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ editText1.setText(buttonView.getText()+"选中"); }else{ editText1.setText(buttonView.getText()+"取消选中"); } } }); shenzhen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ editText1.setText(buttonView.getText()+"选中"); }else{ editText1.setText(buttonView.getText()+"取消选中"); } } }); } }
android怎么把checkbox状态设置为选中状态
android:checked="true"通过checkbox.isChecked();是判断后是否选中,
checkbox.setChecked(true|false)是赋某个checkbox选中。
0 0
- android CheckBox控件的定义及事件监听
- android CheckBox控件的定义及事件监听
- android CheckBox控件的定义及事件监听
- android CheckBox控件的定义及事件监听
- android各种控件的事件监听及举例
- android各种控件的事件监听及举例
- android各种控件的事件监听及举例
- Android各种控件的事件监听
- android-各种控件的监听事件 - 随心
- Android各种控件的事件监听 (转)
- Android各种控件的事件监听 (转)
- Android控件——RadioGroup/RadioButton定义及监听
- 控件的监听事件。
- android EditText控件事件监听
- android EditText控件事件监听
- android EditText控件事件监听
- android EditText控件事件监听
- android EditText控件事件监听
- Windows 下配置 Python 和 MariaDb
- Android清除本地缓存工具类代码
- Centos7.2下使用YUM快速安装MySQL5.7
- 多行多余文字省略号
- Deep Learning 学习系列004 —— 结构设计
- android CheckBox控件的定义及事件监听
- leetcode:Reverse Integer
- SQL Trace
- 在Android中,如何使屏幕常亮的问题
- 写给 iOS 程序员看的 C++(1)
- 建议61:避免在finally内撰写无效代码
- Android OpenGL ES 简明开发教程_材质渲染
- 设计模式——命令模式
- Android开发注意事项--不定时更新