`
guochongcan
  • 浏览: 320776 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

android 中checkbox的使用

 
阅读更多

主要是讲checkbox的使用,其实接触过html都应该知道表单中checkbox选择按钮,在这里我在一个activity中添加4个checkbox和一个button主要是通过button触发事件获取选中的checkbox中的值,我定义了一个checkbox.xml的应该layout布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	android:orientation="vertical">
	<CheckBox android:id="@+id/plain_cb" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="Plain" />
	<CheckBox android:id="@+id/serif_cb" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="Serif" />
	<CheckBox android:id="@+id/bold_cb" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="Bold" />
	<CheckBox android:id="@+id/italic_cb" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="Italic" />

	<Button android:id="@+id/getValue" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="获取CheckBox值" />
</LinearLayout> 

 

 

 

下面是实现代码 :

public class CheckBoxActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.checkbox);

		final CheckBox plain = (CheckBox) findViewById(R.id.plain_cb);
		final CheckBox serif = (CheckBox) findViewById(R.id.serif_cb);
		final CheckBox bold = (CheckBox) findViewById(R.id.bold_cb);
		final CheckBox italic = (CheckBox) findViewById(R.id.italic_cb);

		Button getValue = (Button) findViewById(R.id.getValue);

		getValue.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String title = "你选择了";
				if (plain.isChecked()) {
					title += "plain";
				}
				if (serif.isChecked()) {
					title += "serif";
				}
				if (bold.isChecked()) {
					title += "bold";
				}
				if (italic.isChecked()) {
					title += "italic";
				}
				setTitle(title);

			}
		});
	}

}

 

 

==========================================================

 

下面是另一个

 

<?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">
	<TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
	<CheckBox android:text="@+id/CheckBox01" android:id="@+id/CheckBox01"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
	<CheckBox android:text="@+id/CheckBox02" android:id="@+id/CheckBox02"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
	<CheckBox android:text="@+id/CheckBox03" android:id="@+id/CheckBox03"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
	<CheckBox android:text="@+id/CheckBox04" android:id="@+id/CheckBox04"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
	<Button android:text="@+id/Button01" android:id="@+id/Button01"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>  

 

 

 

 

public class CheckBoxDemo extends Activity {

	private TextView m_txtView;
	private CheckBox m_CheckBox1;
	private CheckBox m_CheckBox2;
	private CheckBox m_CheckBox3;
	private CheckBox m_CheckBox4;
	private Button m_Button;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		m_txtView = (TextView) this.findViewById(R.id.TextView01);

		m_CheckBox1 = (CheckBox) this.findViewById(R.id.CheckBox01);
		m_CheckBox2 = (CheckBox) this.findViewById(R.id.CheckBox02);
		m_CheckBox3 = (CheckBox) this.findViewById(R.id.CheckBox03);
		m_CheckBox4 = (CheckBox) this.findViewById(R.id.CheckBox04);

		m_txtView.setText("调查:你喜欢Android是因为什么原因?");
		m_CheckBox1.setText("好看");
		m_CheckBox2.setText("好用");
		m_CheckBox3.setText("免费");
		m_CheckBox4.setText("应用广泛");

		m_CheckBox1.setOnCheckedChangeListener(m_checkboxListener);
		m_CheckBox2.setOnCheckedChangeListener(m_checkboxListener);
		m_CheckBox3.setOnCheckedChangeListener(m_checkboxListener);
		m_CheckBox4.setOnCheckedChangeListener(m_checkboxListener);

		m_Button = (Button) this.findViewById(R.id.Button01);
		m_Button.setOnClickListener(m_BtnListener);
		m_Button.setText("提交");
	}

	private OnClickListener m_BtnListener = new OnClickListener() {
		public void onClick(View arg0) {
			// TODO Auto-generated method stub

			if (arg0.getId() == R.id.Button01) {
				int value = 0;

				if (m_CheckBox1.isChecked()) {
					value++;
				}
				if (m_CheckBox2.isChecked()) {
					value++;
				}
				if (m_CheckBox3.isChecked()) {
					value++;
				}
				if (m_CheckBox4.isChecked()) {
					value++;
				}

				Toast.makeText(getBaseContext(), "你选择了 " + value + "项",
						Toast.LENGTH_SHORT).show();

			}
		}
	};
	private CheckBox.OnCheckedChangeListener m_checkboxListener = new CheckBox.OnCheckedChangeListener() {
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub

			if (buttonView.getId() == R.id.CheckBox01) {
				if (isChecked) {
					Toast.makeText(getBaseContext(), "CheckBox 01 check ",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getBaseContext(), "CheckBox 01 ucheck ",
							Toast.LENGTH_SHORT).show();
				}
			}
			if (buttonView.getId() == R.id.CheckBox02) {
				if (isChecked) {
					Toast.makeText(getBaseContext(), "CheckBox 02 check ",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getBaseContext(), "CheckBox 02 ucheck ",
							Toast.LENGTH_SHORT).show();
				}
			}
			if (buttonView.getId() == R.id.CheckBox03) {
				if (isChecked) {
					Toast.makeText(getBaseContext(), "CheckBox 03 check ",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getBaseContext(), "CheckBox 03 ucheck ",
							Toast.LENGTH_SHORT).show();
				}
			}
			if (buttonView.getId() == R.id.CheckBox04) {
				if (isChecked) {
					Toast.makeText(getBaseContext(), "CheckBox 04 check ",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getBaseContext(), "CheckBox 04 ucheck ",
							Toast.LENGTH_SHORT).show();
				}
			}

		}

	};

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics