1

我有一个按钮,当我单击该按钮时会打开一个对话框。在该对话框中,一个 EditText 和 ok 按钮,当我在该文本显示中单击 ok 按钮时,我输入了一些文本。
我不知道该怎么做,帮帮我

4

6 回答 6

3

使用以下

 import android.app.Activity;
 import android.app.AlertDialog;
  import android.content.Context;
      import android.content.DialogInterface;
   import android.os.Bundle;
    import android.view.LayoutInflater;
 import android.view.View;
    import android.view.View.OnClickListener;
   import android.widget.Button;
    import android.widget.EditText;

 public class MainActivity extends Activity {

final Context context = this;
private Button button;
private EditText result;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // components from main.xml
    button = (Button) findViewById(R.id.buttonPrompt);
    result = (EditText) findViewById(R.id.editTextResult);

    // add button listener
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // get prompts.xml view
            LayoutInflater li = LayoutInflater.from(context);
            View view= li.inflate(R.layout.prompts, null);

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

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

            final EditText userInput = (EditText) view
                    .findViewById(R.id.editTextDialogUserInput);

            // 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();

        }
    });
}

}

于 2013-03-25T07:44:36.137 回答
2

希望对你有帮助

 AlertDialog.Builder alert = new AlertDialog.Builder(con);
                final EditText input = new EditText(con);
                String s="your text";

                alert.setView(input);
                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String value = input.getText().toString().trim();
                        Toast.makeText(con, value, Toast.LENGTH_SHORT).show();
                    }
                });
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                });
                alert.show();
于 2013-03-25T07:46:26.010 回答
1
public class MainActivity extends Activity {

final Context context = this;
private Button button;
private TextView result;
private TextView result2;

public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 // components from main.xml
 button = (Button) findViewById(R.id.button1);
 result = (TextView) findViewById(R.id.textView1);
 result2 = (TextView) findViewById(R.id.textView2);

 // add button listener
 button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {

       // get prompts.xml view
       LayoutInflater li = LayoutInflater.from(context);
       View view= li.inflate(R.layout.dailog, null);

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

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

       final EditText userInput = (EditText) view
               .findViewById(R.id.edit1);
       final EditText userInput2 = (EditText) view
               .findViewById(R.id.edit2);

       // 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());
               result2.setText(userInput2.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();

   }
 });
}

}

这是我的确切答案

于 2013-03-25T08:41:59.390 回答
1

我会用尽可能少的代码带你完成这些步骤,因为我认为这会帮助你了解更多(比这里的其他答案):

  • 在需要启动对话框的按钮上设置 OnClickListener
  • 在 XML 中为对话框创建自定义视图,并将其命名为 custom_box.xml
  • 之后,在您的 OnClick 方法中,使用以下命令将该自定义 xml 设置为对话框:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_box);
  • 在 Java 中连接 Button 和 EditText 就像在 Activity 中一样,但现在在 Activity 中使用 dialog.findViewById(R.id...) 而不是 this.findViewById(...)
  • 为对话框内的按钮设置 OnClickListener,并从那里调用解除和更改文本操作。

如果您需要,这里也是一个相当不错的教程的链接,这里是Android 文档

于 2013-03-25T07:49:16.550 回答
0
Call ForgetPswPopUp() method name


 public void ForgetPswPopUp() {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                        HomeActivity.this);
                alertDialog.setTitle("set Title");
                final EditText input = new EditText(HomeActivity.this);
                input.setHint("Textbox hint");
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, position);
                input.setLayoutParams(lp);
                alertDialog.setView(input);
                alertDialog.setNegativeButton("Button name",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                alertDialog.show();
            }
于 2014-12-02T05:30:58.783 回答
0

是解释如何在android中创建自定义对话框的链接,这将对您有所帮助

于 2013-03-25T07:46:02.300 回答