有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我希望电话号码被加载到字符串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:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/relativeLayout3"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content" >

    <EditText  
        安卓:layout_height="wrap_content"  
        安卓:id="@+id/phoneno"  
        安卓:inputType="text|phone"  
        安卓:layout_width="wrap_content"  
        安卓:layout_toLeftOf="@+id/Contacts"  
        安卓:layout_alignParentLeft="true"/>
    <Button  
        安卓:layout_width="wrap_content"  
        安卓:layout_height="wrap_content"  
        安卓:id="@+id/Contacts"  
        安卓:text="Contacts"  
        安卓:layout_alignParentRight="true" 
        安卓:onClick="doLaunchContactPicker"/>
    <TextView 
        安卓:text="TextView" 
        安卓:layout_width="wrap_content" 
        安卓:id="@+id/textView1" 
        安卓:layout_height="wrap_content" 
        安卓:layout_below="@+id/phoneno" 
        安卓:layout_alignParentLeft="true"/>

</RelativeLayout>

下一次尝试

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:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/relativeLayout3"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content" >
<EditText 安卓:layout_width="wrap_content" 安卓:layout_height="wrap_content" 安卓:id="@+id/editText1" 安卓:layout_alignParentTop="true" 安卓:layout_alignParentLeft="true">
    <requestFocus></requestFocus>
</EditText>
<Button 安卓:id="@+id/button1" 安卓:layout_width="wrap_content" 安卓:text="Button" 安卓:layout_height="wrap_content" 安卓:layout_alignParentTop="true" 安卓:layout_toRightOf="@+id/editText1" 安卓:layout_marginLeft="33dp"></Button>
<TextView 安卓:text="TextView" 安卓:layout_width="wrap_content" 安卓:id="@+id/textView1" 安卓:layout_height="wrap_content" 安卓:layout_alignParentBottom="true" 安卓:layout_centerHorizontal="true"></TextView>
 </RelativeLayout>

共 (1) 个答案

  1. # 1 楼答案

    在onCreate()方法中的setContentView(R.layout.main);之后调用这个doLaunchContactPicker()方法,如果在doLaunchContactPicker()方法中没有使用视图,那么不要将View作为方法中的参数传递,并在setContentView(R.layout.main);之后添加这一行Button btn=(Button) findViewById(R.id.Contacts);