5

我想DialogEditText. 我设想它有两个按钮:一个肯定按钮,它将保留包含在 中的文本EditText,以及一个否定按钮,它将取消并返回Activity启动它的按钮,就好像什么都没发生一样。我试过使用AlertDialog没有成功:

AlertDialog.Builder builder = new Builder(context);
final EditText text = new EditText(context);

builder.setTitle("New Profile").setMessage("Name this new profile").setView(text);
builder.setPositiveButton("Create", new OnClickListener() {

    public void onClick(DialogInterface di, int i) {
        final String name = text.getText().toString();
        //do something with it
    }
});
builder.setNegativeButton("Cancel", new OnClickListener() {

    public void onClick(DialogInterface di, int i) {
    }
});
builder.create().show();

我的setView()方法有问题吗?使用自定义Dialog会更实用吗?

编辑:

我得到了几个答案,说代码对他们有用……不知道为什么它对我不起作用。这是日志猫:

E/AndroidRuntime(  326): FATAL EXCEPTION: main
E/AndroidRuntime(  326): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime(  326):    at android.view.ViewRoot.setView(ViewRoot.java:531)
E/AndroidRuntime(  326):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
E/AndroidRuntime(  326):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/AndroidRuntime(  326):    at android.app.Dialog.show(Dialog.java:241)
E/AndroidRuntime(  326):    at com.gobernador.TopPage.onListItemClick(TopPage.java:77)
E/AndroidRuntime(  326):    at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
E/AndroidRuntime(  326):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
E/AndroidRuntime(  326):    at android.widget.ListView.performItemClick(ListView.java:3513)
E/AndroidRuntime(  326):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
E/AndroidRuntime(  326):    at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(  326):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(  326):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  326):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(  326):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  326):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(  326):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(  326):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(  326):    at dalvik.system.NativeStart.main(Native Method)
4

4 回答 4

2

gobernador Agarwal 是对的,我也试过你的代码并且它正在工作,通过用这个替换上下文来尝试它

AlertDialog.Builder builder = new Builder(this);
        final EditText text = new EditText(this);

        builder.setTitle("New Profile").setMessage("Name this new profile").setView(text);
        builder.setPositiveButton("Create", new OnClickListener() {

            public void onClick(DialogInterface di, int i) {
                final String name = text.getText().toString();
                //do something with it
            }
        });
        builder.setNegativeButton("Cancel", new OnClickListener() {

            public void onClick(DialogInterface di, int i) {
            }
        });
        builder.create().show();
于 2012-04-04T06:01:19.967 回答
1

试试这个,它对我有用

         AlertDialog.Builder alert = new AlertDialog.Builder(this);
         alert.setTitle("Title");
         alert.setMessage("Message");
        // Set an EditText view to get user input 
       final EditText input = new EditText(this);
       alert.setView(input);
       alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {
                  String value = input.getText();
                 // Do something with value!
       }
       });

      alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
             // Canceled.
      }
     });

     alert.show();
于 2012-04-04T04:23:51.353 回答
1

您可以使用自定义对话框而不是AlertDialog这样

Dialog dilog=new Dialog(this);
dilog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dilog.setContentView(R.layout.yourlayout_in_xml);

并参考其领域

EditText youredittext=(EditText)dilog.findViewById(R.id.youredittext);
String textGet = youredittext.getText().toString(); // u will get ur inserted stting here

dilog.show() to show it
于 2012-04-04T04:35:19.057 回答
0

您需要将自定义视图放置在对话框中。您可以动态创建视图或从 XML 扩展它。您将要使用而不是setTitlesetNegativeButton等的方法是setView

于 2012-04-04T04:18:38.493 回答