gpt4 book ai didi

java - onActivityResult中的requestCode和resultCode指的是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:51 24 4
gpt4 key购买 nike

大家好。我是 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等。

关于java - onActivityResult中的requestCode和resultCode指的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33382244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com