此代码仅设法在另一个活动的textview上仅显示一个复选框的文本。如果有人能帮助我,我真的很感激。 P / S:我是新手。
这是我目前在MainActivity上的代码:
package com.example.ujianrisiko;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class MainActivity extends Activity {
//protected static final String selectedItemData = null;
Button button1;
CheckBox checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8, checkBox9, checkBox10, checkBox11, checkBox12, checkBox13, checkBox14, checkBox15, checkBox16, checkBox17;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#E71E67")));
button1 = (Button) findViewById(R.id.button1);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
checkBox5 = (CheckBox) findViewById(R.id.checkBox5);
checkBox6 = (CheckBox) findViewById(R.id.checkBox6);
checkBox7 = (CheckBox) findViewById(R.id.checkBox7);
checkBox8 = (CheckBox) findViewById(R.id.checkBox8);
checkBox9 = (CheckBox) findViewById(R.id.checkBox9);
checkBox10 = (CheckBox) findViewById(R.id.checkBox10);
checkBox11 = (CheckBox) findViewById(R.id.checkBox11);
checkBox12 = (CheckBox) findViewById(R.id.checkBox12);
checkBox13 = (CheckBox) findViewById(R.id.checkBox13);
checkBox14 = (CheckBox) findViewById(R.id.checkBox14);
checkBox15 = (CheckBox) findViewById(R.id.checkBox15);
checkBox16 = (CheckBox) findViewById(R.id.checkBox16);
checkBox17 = (CheckBox) findViewById(R.id.checkBox17);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Start email activity using explicit intent
//new Intent (From where, To where);
Intent i=new Intent(MainActivity.this,AdviceActivity.class);
i.putExtra("selected", checkBox1.getText().toString());
startActivity(i);
}
});
}
}
这是我的代码另一项活动:
package com.example.ujianrisiko;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
public class AdviceActivity extends Activity {
TextView textView1, textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advice);
textView1= (TextView) findViewById(R.id.textView1);
textView2= (TextView) findViewById(R.id.textView2);
//Bundle b = getIntent().getExtras();
//String selection = b.getString("selection");
String selecteditem=getIntent().getExtras().getString("selected");
textView2.setText(selecteditem);
}
}
答案 0 :(得分:0)
您可以使用StringBuilder
以这种方式存储所有复选框值
public static final String SEPERATOR = ",";
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(checkBox1.getText().toString());
stringBuilder.append(SEPERATOR + checkBox2.getText().toString());
stringBuilder.append(SEPERATOR + checkBox3.getText().toString());
...
并发送您的数据
Intent i=new Intent(MainActivity.this,AdviceActivity.class);
i.putExtra("selected", stringBuilder.toString());
startActivity(i);
然后您必须拆分数据并将其存储在String Array
中。
String[] datas = getIntent().getExtras().getString("selected").split(SEPERATOR);
在datas
中,您可以获得所有值。