大家好 我试图从第一个(主要)活动中的第二个活动中获取值。因为我在firstactivity中使用onActivityResult。它仅在第一次从第二次活动返回第一(主)活动的值。再次,我来到firstactivity以前只显示的值。
我的申请流程:
一阶>二线>首先
第一(主)活性:
public class main extends Activity {
/** Called when the activity is first created. */
LinearLayout l2;
TextView tv2;
String check=null;
// static int value=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l2 = (LinearLayout)findViewById(R.id.linearLayout2);
tv2=(TextView)findViewById(R.id.textView2);
l2.setClickable(true);
l2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(view.getContext(), secondactivity.class);
startActivityForResult(myIntent, 0);
}
});
}
@Override
public void onActivityResult(int reqCode, int resCode, Intent data)
{
Log.e("runapp1","onActivityResult");
Bundle bundle = data.getExtras();
check = bundle != null ? bundle.getString("key1") : "";
Log.e("runapp1","onActivityResult value "+check);
tv2.setText(check);
}
}
在第二项活动中:
public class secondactivity extends Activity {
/** Called when the activity is first created. */
CheckBox c1,c2;
TextView t;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
c1=(CheckBox)findViewById(R.id.checkBox1);
c2=(CheckBox)findViewById(R.id.checkBox2);
c1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
c1.setChecked(true);
Bundle bundle = new Bundle();
bundle.putString("key1",String.valueOf(c1.getText()));
Intent myIntent = new Intent(view.getContext(), main.class);
myIntent.putExtras(bundle);
setResult(RESULT_OK, myIntent);
finish();
}
});
c2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
c2.setChecked(true);
Bundle bundle = new Bundle();
bundle.putString("key1",String.valueOf(c1.getText()));
Intent myIntent = new Intent(view.getContext(), main.class);
myIntent.putExtras(bundle);
setResult(RESULT_OK, myIntent);
finish();
}
});
}
}
请帮助我。
答案 0 :(得分:0)
您需要执行以下操作,具体取决于您要捕获复选框值的位置。
c1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (c1.isChecked())
{
Bundle bundle = new Bundle();
bundle.putString("key1",String.valueOf(c1.getText()));
Intent myIntent = new Intent();
myIntent.putExtras(bundle);
setResult(RESULT_OK, myIntent);
finish();
}
}
});