第一步:在main_xml文件中的代码:
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.yangjian.learncomponents.MulChoose"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="你喜欢吃那些食物?" android:id="@+id/textView2" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="红烧肉" android:id="@+id/cb1" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="苹果" android:id="@+id/cb2" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="火龙果" android:id="@+id/cb3" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="面条" android:id="@+id/cb4" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/tvResult" /> </LinearLayout>
第二步:在MainActivity中的代码:
package com.example.yangjian.learncomponents; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; public class MulChoose extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener { private CheckBox cb1, cb2, cb3, cb4; private TextView tvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mul_choose); tvResult = (TextView) findViewById(R.id.tvResult); cb1 = (CheckBox) findViewById(R.id.cb1); cb2 = (CheckBox) findViewById(R.id.cb2); cb3 = (CheckBox) findViewById(R.id.cb3); cb4 = (CheckBox) findViewById(R.id.cb4); cb1.setOnCheckedChangeListener(this); cb2.setOnCheckedChangeListener(this); cb3.setOnCheckedChangeListener(this); cb4.setOnCheckedChangeListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_mul_choose, 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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { String str = "你喜欢"; if (cb1.isChecked()){ str+= cb1.getText()+","; } if (cb2.isChecked()){ str+= cb2.getText()+","; } if (cb3.isChecked()){ str+= cb3.getText()+","; } if (cb4.isChecked()){ str+= cb4.getText(); } tvResult.setText(str); } }