简体   繁体   中英

How to get value from another method before main activity is processed?

I want the phonenumber to be loaded into string xx"...how is it possible??How to share data between methods?? I have tried to make the method public but that wont help....I know the activity starts from main activity...so how is it possible to get data from another method and then process main activity?

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 Code:-

<?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 code

<?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>

尝试在setContentView之后doLaunchContactPickeronCreate调用doLaunchContactPicker方法

Call this doLaunchContactPicker() method after setContentView(R.layout.main); in onCreate() method and if you don't use view in doLaunchContactPicker() method then don't pass View as a parameter in method and add this line Button btn=(Button) findViewById(R.id.Contacts); after setContentView(R.layout.main);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM