首页 文章

如何在处理主要活动之前从另一个方法获取值?

提问于
浏览
0

我希望将phonenumber加载到字符串xx中“......怎么可能?如何在方法之间共享数据?我试图让方法公开但不会有帮助....我知道活动从主要活动...那么如何从另一个方法获取数据然后处理主要活动呢?

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"/>

</RelativeLayout>

Next Try

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;
        Button b;
TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                b=(Button) findViewById(R.id.button1);
        tv=(TextView) findViewById(R.id.textView1);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                doLaunchContactPicker(v);

            }
        });



        String xx=phoneTxt.getText().toString();
        tv.setText(xx);
}


    @Override
    public 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.editText1);

                           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();  
                }
             }
    }
    }

}

HTML代码

<?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_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText1" android:layout_alignParentTop="true" android:layout_alignParentLeft="true">
    <requestFocus></requestFocus>
</EditText>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:text="Button" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/editText1" android:layout_marginLeft="33dp"></Button>
<TextView android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"></TextView>
 </RelativeLayout>

2 回答

  • 0

    尝试从 onCreate 调用 doLaunchContactPicker 方法,在 setContentView 之后

  • 0

    在onCreate()方法中 setContentView(R.layout.main); 之后调用此 doLaunchContactPicker() 方法,如果不在 doLaunchContactPicker() 方法中使用view,则不要将 View 作为方法中的参数传递,并在 setContentView(R.layout.main); 之后添加此行 Button btn=(Button) findViewById(R.id.Contacts);

相关问题