android复选框标签,Android中的复选框的使用
復(fù)選框的使用和單選框是不同的對(duì)于單選框來(lái)說(shuō)呢,是必須要分組的每一組內(nèi)單選框只能有一個(gè)被選中,而對(duì)于復(fù)選框來(lái)說(shuō)沒(méi)有組這個(gè)概念,因?yàn)槊恳粋€(gè)都可同時(shí)被選中或者不被選中,復(fù)選框其實(shí)可以看成一個(gè)一般的按鈕,只是多了選中和沒(méi)有選中的狀態(tài)。
基于上面的不同所以單選框和復(fù)選框的事件堅(jiān)挺接口是不同的
單選框的事件監(jiān)聽(tīng)接口是?RadioGroup.OnCheckedChangeListener
復(fù)選框的事件監(jiān)聽(tīng)接口是?CompoundButton.onCheckcedChangeListener
下面是一個(gè)簡(jiǎn)單的復(fù)選框的小程序 ,實(shí)現(xiàn)的效果是在選中愛(ài)好的時(shí)候在屏幕上顯示出來(lái),取消選擇的時(shí)候從屏幕上消失 的程序:
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("愛(ài)好是:\t");
sw=(CheckBox)
this.findViewById(R.id.swim);
bs=(CheckBox)
this.findViewById(R.id.basketball);//**************
sw.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
//添加內(nèi)嵌監(jiān)聽(tīng)器
@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() {
//給復(fù)選框添加內(nèi)部監(jiān)聽(tīng)
@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位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的android复选框标签,Android中的复选框的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android 黑边边框,手机屏幕边缘的
- 下一篇: android sina oauth2.