如何在DialogBox中放置EditText?

时间:2014-01-27 01:00:31

标签: java android android-edittext dialog

我有这段代码:

<EditText
        android:id="@+id/etC" 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:hint="@string/amount_credits"
        android:inputType="number"
        android:imeOptions="actionDone" 
        android:singleLine="true" />

    AlertDialog.Builder creditsBox = new AlertDialog.Builder(this);
    edC = (EditText) findViewById(R.id.etC); 
    creditsBox.setView(edC); //THIS LINE MAKES THE APP CRASH
    creditsBox.setMessage("Enter amount of credits")
      .setCancelable(false)
       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub

        }
    });
    AlertDialog creditsBoxC = creditsBox.create(); 
    creditsBoxC.show();

现在我不明白问题出在哪里。有人可以帮我吗?感谢。

4 个答案:

答案 0 :(得分:0)

我认为您无法像这样设置 EditText

试试这个:

  • 创建新布局(包含EditText)
  • 夸大布局
  • 设置AlertDialog的布局

以下是res/layout/prompts.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <EditText
        android:id="@+id/edt_nm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />

    </EditText>

</LinearLayout>

活动

LayoutInflater li = LayoutInflater.from(context);
                View promptsView = li.inflate(R.layout.prompts, null);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);

                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);

                final EditText userInput = (EditText) promptsView
                        .findViewById(R.id.edt_nm);

                // set dialog message
                alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                        // get user input and set it to result
                        // edit text
                        result.setText(userInput.getText());
                        }
                      })
                    .setNegativeButton("Cancel",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                        }
                      });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

答案 1 :(得分:0)

我猜你在“View has have a parent”的某处出现了错误信息 问题是,每次构建警报时,您都尝试重用相同的EditText对象。 因此,要么通过edc = new EditText();动态创建一个新的EditText对象 或使用inflate()方法而不是findViewById(),因为每次充气都会为您提供一个新的实例。

答案 2 :(得分:0)

试试这个

EditText etC;

LayoutInflater factory = LayoutInflater.from(yourActivity.this);
final View textEntryView = factory.inflate(R.layout.yourlayout, null);          
etC= (EditText)textEntryView.findViewById(R.id.etC);

new AlertDialog.Builder(yourActivity.this)
.setTitle("xx")
.setIcon(R.drawable.logo3)
.setView(textEntryView)
.....

答案 3 :(得分:0)

参见此示例

LayoutInflater li = LayoutInflater.from(classname.this);    
View dialogView =li.inflate(R.layout.common_dialog_layout, null);
AlertDialog.Builder dialogwindow = new AlertDialog.Builder(classname.this);
                                dialogwindow.setView(dialogView);_addEditText = (EditText) dialogView.findViewById(R.id.editTextDialogtext);
_dialogTextView=(TextView)dialogView.findViewById(R.id.dialog_textview);

这里是xml类

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/dialog_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Type Your Text : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextDialogtext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />

    </EditText>