我發佈了一個對話框窗口通過處理程序到用戶界面,並希望從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>
你可以發佈label_dialog.xml嗎? – Raghavendra
嘗試使用最終的'EditText edt =(EditText)dialogview。 findViewById(R.id.label_dialog);' – vijaypalod
問題已解決,但我會發布label_dialog.xml以防萬一。 – rozsatib