Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java (EditText)Android联系人列表获取电话号码_Java_Android_Android Layout_Android Intent_Android Listview - Fatal编程技术网

Java (EditText)Android联系人列表获取电话号码

Java (EditText)Android联系人列表获取电话号码,java,android,android-layout,android-intent,android-listview,Java,Android,Android Layout,Android Intent,Android Listview,我是Android开发新手。我用过; 我在编辑文本中键入号码,现在我想从联系人列表中获取联系人号码。有人能帮我修一下吗 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText pnumber= (EditText) findViewById

我是Android开发新手。我用过;

我在编辑文本中键入号码,现在我想从联系人列表中获取联系人号码。有人能帮我修一下吗

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText pnumber= (EditText) findViewById(R.id.editTextPnumber);
    final EditText gsms= (EditText) findViewById(R.id.editTextSMS);
    Button sendsms= (Button) findViewById(R.id.buttonSend);

    sendsms.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String gPhone= pnumber.getText().toString();
            String gSMS= gsms.getText().toString();


            try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(gPhone, null, gSMS, null, null);

                Toast.makeText(getApplicationContext(), "SMS Sent ! ", Toast.LENGTH_LONG).show();
                finish();

            } catch (Exception e) {
                // TODO: handle exception

                Toast.makeText(getApplicationContext(), "PLS Enter Again ! ", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

        }
    });

如果您的意思是希望将设备上联系人的输入号码与匹配的联系人姓名进行换行? 为了你想要的 1) 联系人提供商 2) 自动完成 3) 如果你不想要一个号码,你可以用一些筹码。(EditText,跨距),

//试试这个方法,我给了您根据需要修改的示例或演示代码。
1.activity_main.xml
2.我的活动
公共类MyActivity扩展了活动{
私人编辑号码;
私人编辑短信;
私人按钮btnSend;
private ImageView imgPickUpContact;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtNumber=(EditText)findViewById(R.id.edtNumber);
edtMessage=(EditText)findViewById(R.id.edtMessage);
btnSend=(按钮)findviewbyd(R.id.btnSend);
imgPickUpContact=(ImageView)findViewById(R.id.imgPickUpContact);
imgPickUpContact.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Intent localcontent=newintent(“android.Intent.action.PICK”,ContactsContract.Contacts.CONTENT\u URI);
startActivityForResult(LocalContent,1);
}
});
btnSend.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
sendSMS(edtNumber.getText().toString(),edtMessage.getText().toString());
}
});
}
ActivityResult上受保护的void(int-requestCode、int-resultCode、Intent-paramIntent){
super.onActivityResult(请求代码、结果代码、paramIntent);
if(resultCode==RESULT\u OK){
字符串str=getPhoneNumber(paramIntent.getData());
如果(str.trim().length()>0){
edtNumber.setText(str);
edtNumber.setSelection(edtNumber.getText().length());
}
}否则{
Toast.makeText(这是“未找到电话号码…”,Toast.LENGTH_SHORT.show();
}
}
私有字符串getPhoneNumber(Uri参数Uri){
字符串id=“”;
字符串编号=”;
Cursor Cursor=getContentResolver().query(paramUri,null,null,null);
while(cursor.moveToNext()){
id=cursor.getString(cursor.getColumnIndex(“\u id”);
if(“1”.equalsIgnoreCase(cursor.getString(cursor.getColumnIndex(“has\u phone\u number”))){
游标cursorNo=getContentResolver().query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,null,“contact\u id=“+id,null,null);
while(cursorNo.moveToNext()){
if(cursorNo.getInt(cursorNo.getColumnIndex(“数据2”))==2){
no=no.concat(cursorNo.getString(cursorNo.getColumnIndex(“data1”));
打破
}
}
cursorNo.close();
}
}
cursor.close();
返回否;
}
私有void sendSMS(字符串paramString1、字符串paramString2){
Intent localIntent=newintent(“android.Intent.action.SENDTO”、Uri.parse(“smsto:+paramString1”);
LocalContent.putExtra(“sms_body”,paramString2);
开始触觉(局部内容);
}
}
3.请在AndroidManifest.xml中定义此权限

这是一个简单的技巧。我希望您能够轻松地将其修复到代码中。 您可以将一个按钮(SMS)放在它的setOnClickListener上,您可以使用它

        public void onClick(View v) {
            // TODO Auto-generated method stub

            try {

                Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                sendIntent.putExtra("sms_body", "default content");
                sendIntent.setType("vnd.android-dir/mms-sms");
                startActivity(sendIntent);



            } catch (Exception e) {
                // TODO: handle exception

                Toast.makeText(getApplicationContext(),
                        "PLS Enter Again ! ", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

        }
    });
        public void onClick(View v) {
            // TODO Auto-generated method stub

            try {

                Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                sendIntent.putExtra("sms_body", "default content");
                sendIntent.setType("vnd.android-dir/mms-sms");
                startActivity(sendIntent);



            } catch (Exception e) {
                // TODO: handle exception

                Toast.makeText(getApplicationContext(),
                        "PLS Enter Again ! ", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

        }
    });