Android开发中复选框的使用

发布于 2019-12-25 13:21:08

Android开发中复选框(CheckBox)的使用方法如下:

1.添加复选框

在新建的工程testApp的activity_main.xml中添加如下代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.testapp.MainActivity" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="广东" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/checkBox1"
        android:layout_below="@+id/checkBox1"
        android:text="河北" />

</RelativeLayout>

在主activity中添加了两个复选框,分别的广东和河北。

代码说明:<CheckBox />标签是添加复选框。

android:id属性设置View的ID。

android:layout_width:设置View宽度

android:layout_height:设置View高度

android:layout_centerInParent="true“ 设置View位于垂直和水平居中位置

android:layout_alignStart="@+id/checkBox1"  设置checkBox2与checkBox1左对齐

android:layout_below="@+id/checkBox1"  设置checkBox2位于checkBox1下方

2.编写复选框按钮更改选择状态的事件响应

在MainActivity.java中添加如下代码

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//定义复选框的选择状态改变的监听者,并显示复选框状态
OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, buttonView.getText().toString()+isChecked, Toast.LENGTH_SHORT).show();
}
};

//获取复选框引用
CheckBox chkBox = (CheckBox)findViewById(R.id.checkBox1);

//为复选框添加监听者
chkBox.setOnCheckedChangeListener(onCheckedChangeListener);

//获取复选框引用

chkBox = (CheckBox)findViewById(R.id.checkBox2);

//为复选框添加监听者
chkBox.setOnCheckedChangeListener(onCheckedChangeListener);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

运行MainActivity.java界面如下:

点击复选框广东

点击复选框河北

去掉广东的选择

去掉河北的选择

————————————————
版权声明:本文为CSDN博主「小亮工作室」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/SmalightLv1981/article/details/49049513

0 条评论

发布
问题

官网
微信

官方微信