2013-10-17 74 views
1

我正在使用警報對話框來編輯textview小部件。彈出對話框時,edittext是否可以用用戶文本填充?我看到有自定義的圖塊和圖標,但沒有輸入消息。 alterdialog是由一個onlcicklistener觸發的,它可以從textview中獲取文本。帶編輯文本的警報對話框構建器

 @Override 
public void onClick(View item) { 

    switch (item.getId()) { 
    case R.id.textView1: 
     strEditText = Title1.getText().toString(); 
     Log.d("TAG", strEditText); 
     strReturn = SetDialogBox(strEditText); 
     Title1.setText(strReturn); 

     break; 

Altertdialog

 private String SetDialogBox(String strEditText) { 

    AlertDialog.Builder alert = new AlertDialog.Builder(this); 

    alert.setTitle("Title"); 
    // alert.setMessage("Message"); 

    // Set an EditText view to get user input 
    final EditText input = new EditText(this); 

    alert.setView(input); 
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      Editable value = input.getText(); 

     // Log.d("TAG", Editable); 

     } 
    }); 

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      // Canceled. 
     } 
    }); 

    alert.show(); 
    return strReturn; 

} 
最終的EditText輸入後

解決文本編輯添加的行=新的EditText(本);

​​3210

回答

1

這應該適用於EDITTEXT也

方法

private void showChooseSuitDialog() { 
     final Dialog chooseSuitDialog = new Dialog(myContext); 
     chooseSuitDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     chooseSuitDialog.setContentView(R.layout.choose_suit_dialog); 
     final Spinner suitSpinner = (Spinner) chooseSuitDialog.findViewById(R.id.suitSpinner); 
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
       myContext, R.array.suits, android.R.layout.simple_spinner_item); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     suitSpinner.setAdapter(adapter); 
     Button okButton = (Button) chooseSuitDialog.findViewById(R.id.okButton); 
     okButton.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View view){ 
       validSuit = (suitSpinner.getSelectedItemPosition()+1)*100; 
       String suitText = ""; 
       if (validSuit == 100) { 
        suitText = "Diamonds"; 
       } else if (validSuit == 200) { 
        suitText = "Clubs"; 
       } else if (validSuit == 300) { 
        suitText = "Hearts"; 
       } else if (validSuit == 400) { 
        suitText = "Spades"; 
       } 
       chooseSuitDialog.dismiss(); 
       Toast.makeText(myContext, "You chose " + suitText, Toast.LENGTH_SHORT).show(); 
       myTurn = false; 
       makeComputerPlay(); 

      } 
     }); 
     chooseSuitDialog.show(); 
    } 

choose_suit_dialog.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
android:id="@+id/chooseSuitLayout" 
android:layout_width="275dp" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:layout_gravity="top" 
xmlns:android="http://schemas.android.com/apk/res/android" 
> 
<TextView 
android:id="@+id/chooseSuitText" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Choose a suit." 
android:textSize="16sp" 
android:layout_marginLeft="5dp" 
android:textColor="#FFFFFF" 
> 
</TextView> 
<Spinner 
android:id="@+id/suitSpinner" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:drawSelectorOnTop="true" 
/> 
<Button 
android:id="@+id/okButton" 
android:layout_width="125dp" 
android:layout_height="wrap_content" 
android:text="OK" 
> 
</Button> 
</LinearLayout> 
+0

我一直在尋找填充與所選的對話框編輯文本窗口。感謝您的示例代碼,但吐司選項不適合我的需求。 – Bobby