我有三个按钮(R.id.1,R.id.2,R.id.3)和三个textView(a,b,c)。如何检查按下了哪个按钮onActivityResult,以便TextView可以相应地设置为按钮?
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode)
{
case 0:
//check which button was pressed
String b = data.getStringExtra("a");
//Apply setText(b);
}
break;
case 1:
//check which button was pressed
String result1=data.getStringExtra("text");
// Apply setText(result);
break;
case 2:
// check which button was pressed
String b2 = data.getStringExtra("a");
// Apply setText (b2);
break;
示例:在案例0中,按下了R.id.1,因此将是a.setText(b)....
If R.id.1 was pressed, a.setText()
If R.id.2 was pressed, b.setText()
If R.id.3 was pressed, c.setText()
代码
Button button1 = (Button) claims.findViewById(R.id.1);
Button button2 = (Button) claims.findViewById(R.id.2);
Button button3 = (Button)claims.findViewById(R.id.3);
a=(TextView)claims.findViewById(R.id.textView1);
b=(TextView)claims.findViewById(R.id.textView2);
c=(TextView)claims.findViewById(R.id.textView3);
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
AlertDialogRadio();
}
};
public void AlertDialogRadio() {
final CharSequence[] ClaimsModel = {"Sunny", "Raining", "Cloudy"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select Weather");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Sunny.class);
startActivityForResult(intent, 0);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Rainy.class);
startActivityForResult(intent, 1);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), Cloudy.class);
startActivityForResult(intent, 2);
}
}
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode)
{
case 0:
//check which button was pressed
String b = data.getStringExtra("a");
//Apply setText(b);
}
break;
case 1:
//check which button was pressed
String result1=data.getStringExtra("text");
// Apply setText(result);
break;
case 2:
// check which button was pressed
String b2 = data.getStringExtra("a");
// Apply setText (b2);
break;
}
被修改
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, requestCode);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
startActivityForResult(intent, requestCode);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
startActivityForResult(intent, requestCode);
} else if (item == 3) {
Intent intent = new Intent(getActivity().getApplicationContext(), Medical.class);
startActivityForResult(intent, requestCode);
} else if (item == 4) {
Intent intent = new Intent(getActivity().getApplicationContext(), Other.class);
startActivityForResult(intent, requestCode);
}
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(getActivity(), "Not coeted ", Toast.LENGTH_LONG).show();
switch (requestCode) {
case requestCode1:
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 requestCode2:
String result1 = data.getStringExtra("text");
String b1 = data.getStringExtra("a");
c.setText(" " + b1 + "------" + "RM " + result1);
break;
case requestCode3:
String result2 = data.getStringExtra("text");
String b2 = data.getStringExtra("a");
c.setText(" " + b2 + "------" + "RM " + result2);
break;
}
}
答案 0 :(得分:2)
int requestCode1 = 1;
int requestCode2 = 2;
int requestCode3 = 3;
Button b1 =(Button) findViewById(R.id.button1);
Button b2 =(Button) findViewById(R.id.button2);
Button b3 =(Button) findViewById(R.id.button3);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialogRadio(requestCode1)
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialogRadio(requestCode2)
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialogRadio(requestCode3)
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == requestCode1){}//button 1
else if(resultCode == requestCode2){}//button 2
else if(resultCode == requestCode3){}//button 3
}
public void AlertDialogRadio(final int requestCode) {
final CharSequence[] ClaimsModel = {"Sunny", "Raining", "Cloudy"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select Weather");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Sunny.class);
startActivityForResult(intent, requestCode);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Rainy.class);
startActivityForResult(intent, requestCode);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), Cloudy.class);
startActivityForResult(intent, requestCode);
}
}
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
答案 1 :(得分:0)
您可以查看“请求代码”。
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
AlertDialogRadio(v.getId());
}
};
public void AlertDialogRadio(int id) {
final CharSequence[] ClaimsModel = {"Sunny", "Raining", "Cloudy"};
switch(id){
case R.id.1:
break;
case R.id.2:
break;
case R.id.3:
break;
}
Bundle bundle = new Bundle();
bundle.putString("key", "value");
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select Weather");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Sunny.class);
startActivityForResult(intent, 0, bundle);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Rainy.class);
startActivityForResult(intent, 1, bundle);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), Cloudy.class);
startActivityForResult(intent, 2, bundle);
}
}
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle bundle = data.getExtras();
// this can any data that you passed
String anyData = bundle.getString();
switch(requestCode)
{
case 0:
//check which button was pressed
String b = data.getStringExtra("a");
//Apply setText(b);
}
break;
case 1:
//check which button was pressed
String result1=data.getStringExtra("text");
// Apply setText(result);
break;
case 2:
// check which button was pressed
String b2 = data.getStringExtra("a");
// Apply setText (b2);
break;
}