将按钮单击到另一个活动时传递复选框数据

时间:2015-10-15 16:40:01

标签: java android android-intent android-activity checkbox

所以我有这个代码,它正在工作。我的问题是如何在新活动中传递和显示结果。我使用selectItinerary

设置了3复选框的onclick
public class topPawikan extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.toppawikan);

    final_text = (TextView)findViewById(R.id.textView);
    final_text.setEnabled(false);
}

public void selectItinerary(View view){
boolean checked = ((CheckBox) view).isChecked();
    switch (view.getId())
    {
        case R.id.checkBox1:

            if (checked)
            {selection.add("Maglibot");}

            else
            {selection.remove("Maglibot");}

            break;


        case R.id.checkBox2:

            if (checked)
            {selection.add("Kumain");}

            else
            {selection.remove("Kumain");}

            break;

        case R.id.checkBox3:

            if (checked)
            {selection.add("Umupo");}

            else
            {selection.remove("Umupo");}

            break;

    }
}

//button
public void finalSelection(View view){
String final_itinerary = "";

    for (String Selections : selection)
    {
        final_itinerary = final_itinerary + Selections + "\n";
    }
    final_text.setText(final_itinerary);
    final_text.setEnabled(true);
}
}

1 个答案:

答案 0 :(得分:0)

将params放入新的Intent中。点击按钮开始新活动。

Intent intent = new Intent(topPawikan.this, SecondActivity.class);
Bundle b = new Bundle();
b.putString("selection", final_text.getText().toString()); 
intent.putExtras(b); 
startActivity(intent);

然后在新活动中获取参数:

Bundle b = getIntent().getExtras();
String selection = b.getString("selection");