【问题标题】:Passing the checkbox data when button is clicked to another activity单击按钮时将复选框数据传递给另一个活动
【发布时间】:2015-10-16 12:40:16
【问题描述】:

所以我有这个代码并且它正在工作。我的问题是如何通过并在新活动中显示结果。我用 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);
}
}

【问题讨论】:

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


【解决方案1】:

将参数放入新的 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");

【讨论】:

  • b.putInt("selection", final_text.getText().toString()); 行有问题'android.os.BaseBundle' 中的'putInt(java.lang.String, int)' 不能应用于'(java.lang.String, java.lang.String)'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多