2016-09-17 105 views
1

我在xml佈局中有一個EditText字段,我將該xml設置爲對話框的佈局。當我點擊EditText字段時,它沒有顯示任何迴應。鍵盤不顯示,光標不顯示。這裏是dialog_layout.xmlEditText字段沒有輸入

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="ADD" 
    android:id="@+id/buttonDialog" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 

<EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/editText" 
    android:layout_above="@+id/buttonDialog" 
    android:layout_centerHorizontal="true"> 
    <requestFocus /> 

</EditText> 




</RelativeLayout> 

這裏是Java代碼:

AddBody.setOnClickListener(new View.OnClickListener(){ 

     @Override 
     public void onClick(View v){ 
      final Dialog dialog = new Dialog(getApplicationContext()); 
      dialog.setContentView(R.layout.dialog_layout); 

      dialog.show(); 

/*rest of the code*/ 
+0

什麼是addbody? – XxGoliathusxX

+0

這是一個按鈕。對話框應該在用戶點擊時打開 –

回答

1

我通常使用AlertDialog來達到同樣的效果。

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
LayoutInflater inflater = getActivity().getLayoutInflater(); 
builder.setView(inflater.inflate(R.layout.dialog_layout, null)) 
     .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int id) { 
      } 
     }) 
     .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
      } 
     }); 
Dialog dialog = builder.create(); 
dialog.show(); 

建議使用AlertDialog與使用Dialog構建對話框佈局相比。