Android 如何从另一种方法中获取价值?如何从另一种方法中获取价值并在主要活动中使用?
我想将电话号码加载到“字符串xx”中…怎么可能?电话号码(取自联系人)会显示在给定的编辑文本中…但我想将相同的号码加载到主活动中的字符串xx中Android 如何从另一种方法中获取价值?如何从另一种方法中获取价值并在主要活动中使用?,android,android-intent,methods,contacts,Android,Android Intent,Methods,Contacts,我想将电话号码加载到“字符串xx”中…怎么可能?电话号码(取自联系人)会显示在给定的编辑文本中…但我想将相同的号码加载到主活动中的字符串xx中 public class MsgcontactActivity extends Activity { private static final int CONTACT_PICKER_RESULT = 1001; public void doLaunchContactPicker(View view) { Intent contactPic
public class MsgcontactActivity extends Activity {
private static final int CONTACT_PICKER_RESULT = 1001;
public void doLaunchContactPicker(View view)
{
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
EditText phoneTxt;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView) findViewById(R.id.textView1);
String xx=phoneTxt.getText().toString();
Toast.makeText(getApplicationContext(), xx,Toast.LENGTH_LONG ).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
{
String phone="";
Cursor contacts=null;
try
{
if (resultCode == RESULT_OK)
{
switch (requestCode)
{
case CONTACT_PICKER_RESULT:
Uri result = data.getData();
String id = result.getLastPathSegment();
contacts=getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);
int phoneIdx = contacts.getColumnIndex(Phone.DATA);
if (contacts.moveToFirst())
{
phone = contacts.getString(phoneIdx);
EditText phoneTxt=(EditText)findViewById(R.id.phoneno);
phoneTxt.setText(phone);
}
else
{
Toast.makeText(this, "error", 100).show();
}
break;
}
}
else
{
Toast.makeText(this, "Warning: activity result not ok",50).show();
}
}
catch (Exception e)
{
Toast.makeText(this, e.getMessage(), 50).show();
}
finally
{
if (contacts != null)
{
contacts.close();
}
}
}
}
}
使用的Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_height="wrap_content"
android:id="@+id/phoneno"
android:inputType="text|phone"
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/Contacts"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Contacts"
android:text="Contacts"
android:layout_alignParentRight="true"
android:onClick="doLaunchContactPicker"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:id="@+id/textView1"
android:layout_height="wrap_content"
android:layout_below="@+id/phoneno"
android:layout_alignParentLeft="true"/>
将xx声明为类变量,并在PhoneTxt.setText(phone)之后写入“xx=phone”。但您将无法在onCreate中使用它。改为创建一个私有方法。为什么我不能在oncreate中使用它??我想在oncreate中专门使用它……这样我就可以将它传递给下一个活动,因为当您在EditText中获得任何内容时,即从选择器获得响应时,oncreate代码已经执行。因此,最好在得到响应后设置xx值\