【问题标题】:(EditText)Android Contact list getting phone number(EditText)Android联系人列表获取电话号码
【发布时间】:2014-02-05 00:33:44
【问题描述】:

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

我在 EditText 中输入号码,现在我想从我的联系人列表中获取联系人号码。有人可以帮我解决这个问题吗?

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();
            }

        }
    });

【问题讨论】:

    标签: java android android-layout android-intent android-listview


    【解决方案1】:

    这是一个简单的技巧。我希望您能够轻松地将其修复到您的代码中。 您可以将一个按钮(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();
                }
    
            }
        });
    

    【讨论】:

      【解决方案2】:

      如果您的意思是您想将输入号码与设备上联系人的匹配联系人姓名相匹配? 为此你想要 1) 联系人提供者http://developer.android.com/guide/topics/providers/contacts-provider.html 2) 自动完成http://developer.android.com/guide/topics/ui/controls/text.html#AutoComplete 3)如果你不想要一个号码,你可以用一些筹码。 (EditText,跨越)https://github.com/nichtemna/ChipsEditText#mychipsedittexthttps://github.com/kpbird/chips-edittext-library

      【讨论】:

        【解决方案3】:
        // try this way here i gave sample or demo code you modify as per your requirement.
        1.activity_main.xml
        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp"
            android:orientation="vertical">
        
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
        
                <EditText
                    android:id="@+id/edtNumber"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:hint="phone number"
                    android:layout_weight="1"/>
                <ImageView
                    android:id="@+id/imgPickUpContact"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher"
                    android:layout_marginLeft="5dp"/>
                </LinearLayout>
        
            <EditText
                android:id="@+id/edtMessage"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="top"
                android:hint="message"/>
            <Button
                android:id="@+id/btnSend"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Send"/>
        
        </LinearLayout>
        
        2.MyActivity
        public class MyActivity extends Activity {
        
            private EditText edtNumber;
            private EditText edtMessage;
            private Button btnSend;
            private ImageView imgPickUpContact;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                edtNumber=(EditText)findViewById(R.id.edtNumber);
                edtMessage=(EditText)findViewById(R.id.edtMessage);
                btnSend=(Button)findViewById(R.id.btnSend);
                imgPickUpContact=(ImageView)findViewById(R.id.imgPickUpContact);
        
                imgPickUpContact.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent localIntent = new Intent("android.intent.action.PICK", ContactsContract.Contacts.CONTENT_URI);
                        startActivityForResult(localIntent, 1);
                    }
                });
        
                btnSend.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        sendSMS(edtNumber.getText().toString(), edtMessage.getText().toString());
                    }
                });
            }
        
            protected void onActivityResult(int requestCode, int resultCode, Intent paramIntent) {
                super.onActivityResult(requestCode, resultCode, paramIntent);
                if (resultCode == RESULT_OK) {
                    String str = getPhoneNumber(paramIntent.getData());
                    if (str.trim().length() > 0) {
                        edtNumber.setText(str);
                        edtNumber.setSelection(edtNumber.getText().length());
                    }
                } else {
                    Toast.makeText(this,"Phone Number Not Founded ...",Toast.LENGTH_SHORT).show();
                }
            }
        
            private String getPhoneNumber(Uri paramUri) {
                String id = "";
                String no="";
                Cursor cursor = getContentResolver().query(paramUri, null, null, null, null);
        
                while(cursor.moveToNext()){
                    id = cursor.getString(cursor.getColumnIndex("_id"));
                    if("1".equalsIgnoreCase(cursor.getString(cursor.getColumnIndex("has_phone_number")))){
                        Cursor cursorNo = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, "contact_id = " + id, null, null);
                        while (cursorNo.moveToNext()) {
                            if (cursorNo.getInt(cursorNo.getColumnIndex("data2")) == 2){
                                no = no.concat(cursorNo.getString(cursorNo.getColumnIndex("data1")));
                                break;
                            }
                        }
                        cursorNo.close();
                    }
                }
                cursor.close();
                return no;
            }
        
            private void sendSMS(String paramString1, String paramString2) {
                Intent localIntent = new Intent("android.intent.action.SENDTO", Uri.parse("smsto:" + paramString1));
                localIntent.putExtra("sms_body", paramString2);
                startActivity(localIntent);
            }
        
        }
        
        3.please define this permission in your AndroidManifest.xml
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.SEND_SMS" />
        

        【讨论】:

          猜你喜欢
          • 2013-05-19
          • 1970-01-01
          • 2017-04-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-01
          • 1970-01-01
          相关资源
          最近更新 更多