简体   繁体   English

如何在Android对话框中显示用户输入的EditText值

[英]How to show user entered EditText value in a android dialogue box

my modified sample of my work basically below i have 2 editboxes i want the values of the both to be shown in an alertbox, rest of the code works fine, the alertbox does show up but without the entered values. 我修改后的工作样本基本上在下面,我有2个编辑框,我希望两者的值都显示在警报框中,其余代码工作正常,警报框的确显示但没有输入值。

public class Main extends Activity {

    EditText username = null;
    EditText password = null;
    Button login;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        EditText usern = (EditText) findViewById(R.id.username);
        EditText pass = (EditText) findViewById(R.id.password);
        Button login = (Button) findViewById(R.id.login);
        String usernm = usern.getText().toString();
        String passnm = pass.getText().toString();
        final Builder alert = new AlertDialog.Builder(this)
            .setTitle("SHOW FIELDS")
            .setMessage("USERNAME:" + usernm + "  PASSWORD:" + passnm)
            .setNegativeButton("CLOSE", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { 
                    // do nothing
                    closeContextMenu();
                }
             });
         login.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 alert.show();
             }
         });

you will need to get EditText's value on button so show in AlertDialog as : 您需要在按钮上获取EditText的值,以便在AlertDialog中显示为:

login.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
          usernm = usern.getText().toString();
          passnm = pass.getText().toString();
             alert.show();
         }

and declare both usernm and passnm at class level 并在类级别声明usernmpassnm

EDIT : or best way create a method to show alert and call it on button click as: 编辑:或最好的方法来创建一种方法来显示警报,并在单击按钮时按以下方式调用它:

  public void showalert(){
          usernm = usern.getText().toString();
          passnm = pass.getText().toString();
          alert = new AlertDialog.Builder(Main.this)
            .setTitle("SHOW FIELDS")
            .setMessage("USERNAME:" + usernm + "  PASSWORD:" + passnm)
            .setNegativeButton("CLOSE", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { 
                    // do nothing
                    closeContextMenu();
                }
             });
     alert.show();
    }

and call this method on button click as: 并在按钮单击为时调用此方法:

 login.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        showalert();
     }:
 });

and declare all variables at class level by which you can access it in whole class as : 并在类级别声明所有变量,您可以按以下方式在整个类中访问它:

public class Main extends Activity {
EditText usern ;
EditText pass ;
Button login;

String usernm ,passnm; //<<< Declare here
Builder alert;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     usern = (EditText) findViewById(R.id.username);
     pass = (EditText) findViewById(R.id.password);
     login = (Button) findViewById(R.id.login);

         login.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
              showalert();
         }
     });
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM