Android Prompt Box
I am new to writing Android Applications, and I need to be able to implement the following functionality. When a user clicks a specific button, I want a customized "prompt" box to appear, cont开发者_如何学编程aining several fields (a textfield, a password field, and a checkbox field). I would also be nice if I could add an image inside this box.
How would I go about implementing this sort of functionality?
Thanks
You need to create a Custom Dialog.
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
where custom_dialog is a layout xml file for the dialog.
Brenna's answer is correct. Also, if your pop-up is extremely complex, you could also create a whole new activity but give it the ThemeDialog theme - that way, it will still look like a dialog on top of your previous activity, and you can give it an additional style to dim or blur the background.
Obviously, this only makes sense if you really need a full new activity (say, if you have lots of code to initialize the popup or validate the input). Otherwise, a simple dialog is a easier.
Btw, I would recommend implementing onCreateDialog and putting Brenna's code in there, that way it will automatically be cleaned up. Otherwise, you may run into problems if the user changes orientation while dealing with your dialog. (Remember that changing orientation will effectively destroy and re-create your current activity.)
精彩评论