简体   繁体   中英

Android Custom dialog box

I have a Button, When I click that button open a dialog box. In that dialog box one EditText and ok button, I am enter some text, when I click ok button in that text displaying.
I don't know How will do that, help me

use the below

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

        }
    });
}

}

Hope this will help you

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

I'll take you through the steps with as little code as possible because I think that'll help you learn more (than the other answer on here):

  • Set an OnClickListener on the Button you need to launch the Dialog Box
  • Create a Custom View for the Dialog Box in XML, and call it custom_box.xml
  • After that, in your OnClick method, use the following to set that custom xml to the dialog:
 final Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.custom_box); 
  • Wire up the Button and EditText in Java as you would for an Activity, but now using dialog.findViewById(R.id...) instead of this.findViewById(...) as in an Activity
  • Set an OnClickListener for the Button inside the dialog box, and call the dismiss and change text action from within there.

Here is a link to a rather good tutorial as well if you need it, and here is the Android documentation .

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

   }
 });
}

}

This my exact Answer

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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