Edittext返回空String

时间:2016-04-27 12:12:47

标签: android string android-edittext

我正在通过处理程序向UI发布一个对话框窗口,并且当我点击OK时想要从EditText中检索String,但它总是返回空字符串。 我的代码:

    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>

2 个答案:

答案 0 :(得分:3)

请从对话框视图中调用EditText,您现在正在活动中引用edittext,但您需要从对话框视图中调用它

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

答案 1 :(得分: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);

快乐的编码。!!!