如何将我的联系人放入edittext?

时间:2015-09-30 17:05:29

标签: android eclipse

GOOD DAY!

我是编程新手,并试图学习新事物。要解释清楚我的问题是我有这个看起来联系的按钮,通过点击联系人,应用程序将显示吐司中的数字。我还无法理解如何使文本在edittext中显示。

这是我的代码。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_s);
    ((Button)findViewById(R.id.btnSelectContact)).setOnClickListener( new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            startActivityForResult(intent, 1);                
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null) {
        Uri uri = data.getData();

        if (uri != null) {
            Cursor c = null;
            try {
                c = getContentResolver().query(uri, new String[]{ 
                            ContactsContract.CommonDataKinds.Phone.NUMBER,  
                            ContactsContract.CommonDataKinds.Phone.TYPE },
                        null, null, null);

                if (c != null && c.moveToFirst()) {
                    String number = c.getString(0);
                    int type = c.getInt(1);
                    showSelectedNumber(type, number);
                }
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

public void showSelectedNumber(int type, String number) {
    Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();
}

我对此进行了深入的搜索,但作为初学者,我需要一些帮助才能了解更多java。谢谢

1 个答案:

答案 0 :(得分:0)

如果您的editText名称为showname,请使用

onCreate()方法

EditText showname = (EditText) findViewById(R.id.xml_id_of_your_text);

并在显示所需文本的方法中

public void showSelectedNumber(int type, String number) {
showname.setText(""+type + ": " + number+"");
}
相关问题