我有3个按钮,当我单击其中一个按钮时,它们正在打开我的联系人列表。然后我选择某人,我得到一个电话号码,我想将该号码设置为按钮的文本。我的问题是:我如何处理该功能中按下的按钮。 Performclick
对我不起作用。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
//for get number and name from contacts
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.Contacts.DISPLAY_NAME },
null, null, null);
if (c != null && c.moveToFirst()) {
number = c.getString(0);
name=c.getString(1);
if(Number1.performClick()){
Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
}else if(Number2.performClick()){
Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
}
else if(Number3.performClick()){
Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();
}
}
}
finally {
if (c != null) {
c.close();
}
}
}
}
试试这个:
//..
int currentButtonId;
//..
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
currentButtonId = R.id.button1;
button1Clicked();
break;
case R.id.button2:
currentButtonId = R.id.button2;
button2Clicked();
break;
case R.id.button3:
currentButtonId = R.id.button3;
button3Clicked();
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (currentButtonId) {
case R.id.button1:
fromButton1();
break;
case R.id.button2:
fromButton2();
break;
case R.id.button3:
fromButton3();
break;
default:
break;
}
}
您可以在
呼叫联系人列表时发送不同的-2 request code
,并在onAcitivityResult()
中检查请求代码。
或者,您可以创建一个static int
变量来保存单击的按钮 ID,并根据此变量设置结果。