大家好。我是 android 新手,现在在我的程序中使用 startActivityForResult 。在我的应用程序中,我有两个按钮和两个 TextView 。用于打开对话框的两个按钮
。如何检查 ActivityResult 上按下了哪个按钮,以便 TextView 可以根据按钮设置文本?
int a1 = 1;
int a2 = 2;
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialogRadio(a1);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialogRadio(a2);
}
});
public void AlertDialogRadio(final int k) { //parameter k is never used
final CharSequence[] ClaimsModel = {"Project", "Petrol", "Car Maintenance"
, "Medical", "Other"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select a Claims");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
startActivityForResult(intent, 0);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
startActivityForResult(intent, 1);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
startActivityForResult(intent, 2);
} else if (item == 3) {
Intent intent = new Intent(getActivity().getApplicationContext(), Medical.class);
startActivityForResult(intent, 3);
} else if (item == 4) {
Intent intent = new Intent(getActivity().getApplicationContext(), Other.class);
startActivityForResult(intent, 4);
}
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 1) { // if button1 was clicked
switch (requestCode) {
case 0:
String result = data.getStringExtra("text");
String b = data.getStringExtra("a");
c.setText(" " + b + "------" + "RM " + result);
Toast.makeText(getActivity(), "Not completed ", Toast.LENGTH_LONG).show();
break;
case 1:
String result1 = data.getStringExtra("text");
String b1 = data.getStringExtra("a");
c.setText(" " + b1 + "------" + "RM " + result1);
break;
case 2:
String result2 = data.getStringExtra("text");
String b2 = data.getStringExtra("a");
c.setText(" " + b2 + "------" + "RM " + result2);
break;
case 3:
String result3 = data.getStringExtra("text");
String b3 = data.getStringExtra("a");
c.setText(" " + b3 + "------" + "RM " + result3);
break;
case 4:
String result4 = data.getStringExtra("text");
String b4 = data.getStringExtra("a");
c.setText(" " + b4 + "------" + "RM " + result4);
break;
}
}
else if (resultCode == 2) { // if button2 was clicked
switch (requestCode) {
case 0:
String result = data.getStringExtra("text");
String b = data.getStringExtra("a");
d.setText(" " + b + "------" + "RM " + result);
break;
case 1:
String result1 = data.getStringExtra("text");
String b1 = data.getStringExtra("a");
d.setText(" " + b1 + "------" + "RM " + result1);
break;
case 2:
String result2 = data.getStringExtra("text");
String b2 = data.getStringExtra("a");
d.setText(" " + b2 + "------" + "RM " + result2);
break;
case 3:
String result3 = data.getStringExtra("text");
String b3 = data.getStringExtra("a");
d.setText(" " + b3 + "------" + "RM " + result3);
break;
case 4:
String result4 = data.getStringExtra("text");
String b4 = data.getStringExtra("a");
d.setText(" " + b4 + "------" + "RM " + result4);
break;
}
}
}
所以我的程序应该像这样工作:如果单击了button1....c.setText();如果单击了button2....d.setText();
但是程序现在TextView 上没有任何显示。错误是否来自 if (resultCode == 1)
和 else if (resultCode == 2)
?非常感谢
假设使用选择Project1.class
Project1.class
public class Project1 extends AppCompatActivity {
private static String text;
private static String a;
private static EditText txt;
private int g;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
txt= (EditText)findViewById(R.id.editText36);
Button b=(Button)findViewById(R.id.button17);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent returnIntent = new Intent();
a="Project";
text = txt.getText().toString();
returnIntent.putExtra("text", text);
returnIntent.putExtra("a",a);
// returnIntent.putExtra("k",getIntent().getExtras().getString("k"));
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
}
}
request_code是调用函数标识,从哪里请求,result_code是被调用函数标识符,它还指定被调用消息的状态,如Intent.ACTIVITY_OK等。
我是一名优秀的程序员,十分优秀!