2016-04-27 202 views
-1

我發佈了一個對話框窗口通過處理程序到用戶界面,並希望從EditText檢索字符串,當我點擊確定,但它總是返回空字符串。 我的代碼:Edittext返回空字符串

public void showLabelDialog() { 
    handler.post(new Runnable() { 
     @Override 
     public void run() { 
      AlertDialog.Builder builder = new AlertDialog.Builder((Activity) getThis()); 
      LayoutInflater inflater = ((Activity) getThis()).getLayoutInflater(); 
      View dialogView = inflater.inflate(R.layout.label_dialog, null); 
      final EditText edt=(EditText) findViewById(R.id.label_dialog); 
      builder.setView(dialogView); 
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        setCurrentLabel(edt.getText().toString()); 
        System.out.println("Current text:" + edt.getText().toString()); 
        labeled = true; 
       } 
      }); 
      builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        setCurrentLabel(null); 
        labeled = true; 
       } 
      }); 
      AlertDialog b = builder.create(); 
      b.show(); 
     } 
    }); 
} 

輸出是:

I/System.out的:當前的文本:

的label_dialog.xml是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="10dp" 
android:orientation="vertical"> 
<EditText 
    android:id="@+id/label_dialog" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:title="@string/label_dialog" 
    android:inputType="text" /> 
</LinearLayout> 
+0

你可以發佈label_dialog.xml嗎? – Raghavendra

+1

嘗試使用最終的'EditText edt =(EditText)dialogview。 findViewById(R.id.label_dialog);' – vijaypalod

+0

問題已解決,但我會發布label_dialog.xml以防萬一。 – rozsatib

回答

3

請從打電話給你EditText你的對話視圖,你現在正在活動中引用edittext,但你需要從你的對話框視圖中調用它。

final EditText edt = (EditText) dialogView.findViewById(R.id.label_dialog);

1

試試這個,

public void showLabelDialog() { 

      AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
      LayoutInflater inflater = (MainActivity.this).getLayoutInflater(); 
      View dialogView = inflater.inflate(R.layout.label_dialog, null); 
      final EditText edt=(EditText)dialogView.findViewById(R.id.label_dialog); 
      builder.setView(dialogView); 
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        //setCurrentLabel(edt.getText().toString()); 
        System.out.println("Current text:" + edt.getText().toString()); 
        //labeled = true; 
       } 
      }); 
      builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        //setCurrentLabel(null); 
        //labeled = true; 
       } 
      }); 
      AlertDialog b = builder.create(); 
      b.show(); 
     } 

即用,

final EditText edt=(EditText)dialogView.findViewById(R.id.label_dialog); 

代替

final EditText edt=(EditText)findViewById(R.id.label_dialog); 

編碼愉快。!