在一个活动中,我们有 3 或 4 个多个复选框和两个按钮,一个用于提交数据,另一个用于清除选定的复选框。单击选中的复选框后,数据应显示在下一页上,任何人都可以提供解决方案吗?
问问题
51 次
1 回答
0
//Demo Cals 调用第二类。
public class DemoActivity extends Activity implements OnClickListener,
OnCheckedChangeListener {
private Button btnSubmit, btnClear;
private CheckBox chk1, chk2, chk3;
private Bundle data = new Bundle();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnClear = (Button) findViewById(R.id.btnClear);
chk1 = (CheckBox) findViewById(R.id.chk1);
chk2 = (CheckBox) findViewById(R.id.chk2);
chk3 = (CheckBox) findViewById(R.id.chk3);
btnSubmit.setOnClickListener(this);
btnClear.setOnClickListener(this);
chk1.setOnCheckedChangeListener(this);
chk2.setOnCheckedChangeListener(this);
chk3.setOnCheckedChangeListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSubmit:
Intent intent = new Intent(DemoActivity.this, secondActivity.class);
intent.putExtras(data);
startActivity(intent);
break;
case R.id.btnClear:
chk1.setChecked(false);
chk2.setChecked(false);
chk3.setChecked(false);
break;
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.chk1:
if (isChecked) {
data.putString("Value1", chk1.getText().toString());
Log.v("chk1", chk1.getText().toString());
} else {
if (data.containsKey("Value1")) {
data.remove("Value1");
}
}
break;
case R.id.chk2:
if (isChecked) {
data.putString("Value2", chk2.getText().toString());
Log.v("chk2", chk2.getText().toString());
} else {
if (data.containsKey("Value2")) {
data.remove("Value2");
}
}
break;
case R.id.chk3:
if (isChecked) {
data.putString("Value3", chk3.getText().toString());
Log.v("chk3", chk3.getText().toString());
} else {
if (data.containsKey("Value3")) {
data.remove("Value3");
}
}
break;
}
}
}
public class secondActivity extends Activity {
private Bundle data;
private TextView txtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
txtView = (TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String Value = "";
data = intent.getExtras();
if (data.containsKey("Value1")) {
Value = data.getString("Value1");
Log.v("Value", Value);
txtView.setText(Value);
}
if (data.containsKey("Value2")) {
Value += data.getString("Value2");
Log.v("Value", Value);
txtView.setText(Value);
}
if (data.containsKey("Value3")) {
Value += data.getString("Value3");
Log.v("Value", Value);
txtView.setText(Value);
}
}
}
//主xml
<?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/chk1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<CheckBox
android:id="@+id/chk2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
<CheckBox
android:id="@+id/chk3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear" />
</LinearLayout>
</LinearLayout>
//第二个xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
于 2012-05-06T07:22:40.403 回答