Edittext.settext()不要在onActivityResult()中显示文本

时间:2017-04-07 06:05:44

标签: android android-edittext

我正在尝试在EditText ActivityResult上添加文字,但它不起作用。这就是我所做的:

  private View GetNumber() {


        LayoutInflater inflater = LayoutInflater.from(getBaseContext());
        viewNumber = inflater.inflate(R.layout.stepper_layout, null, false);

        Number_Edittext = (AppCompatEditText) viewNumber.findViewById(R.id.Pic_Edittext_Number);
        RelativeLayout NumberPickerLayout = (RelativeLayout) viewNumber.findViewById(R.id.NumberPickerLayout);
        RelativeLayout Pic_Number = (RelativeLayout) viewNumber.findViewById(R.id.Pic_Number);
        Button numberNext = (Button) viewNumber.findViewById(R.id.numberNext);
        NumberPickerLayout.setVisibility(View.VISIBLE);

        Pic_Number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
            }
        });

        numberNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num = Number_Edittext.getText().toString();
                if (TextUtils.isEmpty(num)) {
                    verticalStepperForm.setActiveStepAsUncompleted("Please Enter or select a number");
                } else {
                    checkNumber(num);
                }
            }
        });


        return viewNumber;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == CONTACT_PICKER_RESULT) {
            Cursor cursor = null;

            Uri uri = data.getData();
            //Query the content uri
            cursor = getContentResolver().query(uri, null, null, null, null);
            cursor.moveToFirst();
            int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            phoneNo = cursor.getString(phoneIndex);
            Number_Edittext.setText(phoneNo);
        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个..

公共类MainActivity扩展了AppCompatActivity {

private static final int CONTACT_PICKER_RESULT = 100;
// Create the variable
TextView mTextView;
Button pickContactBtn;

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

    // find the text view id here
    mTextView = (TextView) findViewById(R.id.textView1);
    // do your work here
    mTextView.setText("Hello World!");

    pickContactBtn= (Button) findViewById(R.id.pickContactBtn);
    pickContactBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);

        }
    });


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == CONTACT_PICKER_RESULT) {
        Cursor cursor = null;
        Uri uri = data.getData();
        //Query the content uri
        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        String phoneNo = cursor.getString(phoneIndex);
        mTextView.setText(phoneNo);
    }
}

}

<强> XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_world"
        android:textSize="36sp"
        android:textColor="@android:color/holo_blue_dark"
        android:textStyle="bold"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="12dp"
        />

    <Button
        android:id="@+id/pickContactBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignStart="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="18dp"
        android:layout_marginStart="18dp"
        android:layout_marginTop="28dp"
        android:text="Pick Contact" />
</RelativeLayout>