如何使用2-3按钮获取联系人并将其显示在textview上

e3bfsja2  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(140)

在显示屏上,我有3个按钮用于获取电话联系人,第一个按钮工作正常,它获取联系人并显示在文本视图中。但是我还有另外两个按钮来获取联系人,这是我的代码,如果有人知道,请帮助我:)

book_1.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent ( Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult (intent, RESULT_PICK_CONTACT);
            }
        });
        book_2.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent ( Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult (intent, RESULT_PICK_CONTACT);
            }
        });
        book_3.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent ( Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult (intent, RESULT_PICK_CONTACT);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult (requestCode, resultCode, data);
        if(resultCode==RESULT_OK)
        {
            switch (requestCode){
            case RESULT_PICK_CONTACT:
                contactPicked(data);
                break;
            }
        }else{
            Toast.makeText (this, "Failed to pick contact", Toast.LENGTH_SHORT).show ( );
        }
    }

    private void contactPicked(Intent data) {
        Cursor cursor = null;
        try {
            String phoneNo = null;
            Uri uri = data.getData ();
            cursor = getContentResolver ().query (uri, null, null, null, null);
            cursor.moveToFirst ();
            int phoneindex = cursor.getColumnIndex (ContactsContract.CommonDataKinds.Phone.NUMBER);
            phoneNo = cursor.getString (phoneindex);

            regNomor1.getEditText ().setText (phoneNo);

        } catch (Exception e) {
            e.printStackTrace ( );
        }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题