如何将ListView内容放入EditText?

时间:2018-06-27 10:40:35

标签: android

我是Android编程的新手,我正计划创建一个可显示联系人并从中呼叫的应用。

我已经通过列表视图显示了联系人,我希望做的是当我单击某个联系人时,该联系人会弹出到我的编辑文本中,然后单击我的呼叫按钮。 (显示图片)联系人和edittext的屏幕截图

updates Screenshot with call button

有什么办法吗?

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:orientation="vertical"
    android:id="@+id/idLayout">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/idContacts"
        android:textAlignment="center"
        android:textSize="15pt" />

    <EditText
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="51dp"
        android:layout_marginBottom="10dp"
        android:inputType=""
        android:labelFor="@+id/number"
        android:textSize="25sp" />

    <Button
        android:id="@+id/callBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:text="Call" />

    <ListView
        android:id="@+id/idList"
        android:layout_width="400dp"
        android:layout_height="188dp"></ListView>

</LinearLayout>

这是我的MainActivity.java

package com.example.spyk.testmessageandcall;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {

    ListView listview;
    EditText numberTxt;
    private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
    Cursor c;
    ArrayList<String> contacts;


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

        listview = (ListView) findViewById(R.id.idList);

        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS);

        if (permissionCheck == PackageManager.PERMISSION_GRANTED){
            showContacts();
        }
        else{
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_CONTACTS},
                    PERMISSIONS_REQUEST_READ_CONTACTS);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1,contacts);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults){
        if(requestCode == PERMISSIONS_REQUEST_READ_CONTACTS){
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                showContacts();
            }
            else{
                Toast.makeText(this, "Names will display if you grant permission", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void showContacts(){
        c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");

        contacts = new ArrayList<String>();
        while (c.moveToNext()){

            String contactName = c.getString(c.getColumnIndex( ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contacts.add("Name: "+contactName + "\n" + "Phone #: " +phNumber);
        }
        c.close();
    }

}

编辑

我需要的是当我单击某个联系人时,只有其编号才会出现在编辑文本上,这是其变量:

phNumber, variable of the phone number

1 个答案:

答案 0 :(得分:1)

只需在onItemClick中键入此内容即可更改editText的文本

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
             String[] splits = contacts.get(i).split(":");

             numberTxt.setText(splits[splits.length-1])
        }
    });

并在呼叫按钮中单击使用

startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", numberTxt.getText().toString(), null)));