2017-04-07 181 views
-4

我正在嘗試在EditText上添加文本ActivityResult,但它不起作用。這是我做了什麼:Edittext.settext()在onActivityResult()中不顯示文本

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); 
     } 
    } 
+0

好吧,先生,古德運氣和謝謝 –

+0

提示:你沒有按照變量「Number_Edittext」的命名約定,你可以將它命名爲inputPhoneNumber或更好的東西。同樣適用於Pic_Number。您沒有正確處理您的光標,請在操作後關閉光標。 – mpals

+0

當我檢查調試模式Edittext.settext()工作,但donsn't不顯示文本 –

回答

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> 
+0

試試這個.. –

+0

如果答案有用,請補足票數.. –