android复选框标签,Android中的复选框的使用
復選框的使用和單選框是不同的對于單選框來說呢,是必須要分組的每一組內單選框只能有一個被選中,而對于復選框來說沒有組這個概念,因為每一個都可同時被選中或者不被選中,復選框其實可以看成一個一般的按鈕,只是多了選中和沒有選中的狀態。
基于上面的不同所以單選框和復選框的事件堅挺接口是不同的
單選框的事件監聽接口是?RadioGroup.OnCheckedChangeListener
復選框的事件監聽接口是?CompoundButton.onCheckcedChangeListener
下面是一個簡單的復選框的小程序 ,實現的效果是在選中愛好的時候在屏幕上顯示出來,取消選擇的時候從屏幕上消失 的程序:
CheckBoxMainActivity.java文件
package com.checkBox.checkbox;
import org.w3c.dom.Comment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class CheckBoxMainActivity extends Activity {
private TextView text=null;
private CheckBox sw=null;
private CheckBox bs=null;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_check_box_main);//得到控件
text=(TextView)
this.findViewById(R.id.text);
text.setText("愛好是:\t");
sw=(CheckBox)
this.findViewById(R.id.swim);
bs=(CheckBox)
this.findViewById(R.id.basketball);//**************
sw.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
//添加內嵌監聽器
@Override
public void
onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
//
TODO Auto-generated method stub
String
favorite=text.getText().toString();
if(isChecked){
if(favorite.contains("游泳")){
}else{
text.setText(favorite+"\t游泳");
}
}else{
if(favorite.contains("游泳")){
text.setText(favorite.replace("\t游泳", ""));
}
}
}
});
bs.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
//給復選框添加內部監聽
@Override
public void
onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
//
TODO Auto-generated method stub
String
favorite=text.getText().toString();
if(isChecked){
if(favorite.contains("籃球")){
}else{
text.setText(favorite+"\t籃球");
}
}else{
if(favorite.contains("籃球")){
text.setText(favorite.replace("\t籃球", ""));
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds
items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_check_box_main,
menu);
return true;
}
}
MainActivity.xml文件
http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CheckBoxMainActivity" >
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
android:id="@+id/swim"
android:text="游泳"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
/>
android:id="@+id/basketball"
android:text="籃球"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_toRightOf="@id/swim"
/>
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的android复选框标签,Android中的复选框的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 黑边边框,手机屏幕边缘的
- 下一篇: 鸿蒙系统8xmax,华为8月9日发布基于