public class MainActivity extends Activity { protected int i; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); } /* * Text dialog: have its own fixed layout/click the external default to close the dialog/cancel button, if no logical operation is performed, the listening event can be directly written as null */ public void showText(View view) { // 1. Create a constructor object for AlertDialog Builder builder = new AlertDialog.Builder(MainActivity.this); // 2. Set the content of the dialog builder.setTitle("prompt box");//Title builder.setIcon(R.drawable.ic_launcher);//图标 builder.setMessage("A simple prompt box");//Content // set the ok (positive) button builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Logical operation performed by click listener Toast.makeText(MainActivity.this, "Clicked the OK button", 0).show(); } }); // set the cancel (negative) button builder.setNegativeButton("取消", null); // 3. Display the dialog builder.show(); } /* * Pop-up radio dialog: */ public void showSingle(View view) { Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Please select gender");//Title builder.setIcon(R.drawable.ic_launcher);//图标 /** * items: all options ---- array checkedItem: the subscript of the item selected by default listener: listening for events */ final String[] items={"male","female","shemale"}; builder.setSingleChoiceItems(items, 2, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { i=which; } }); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "Selected:" + items[i], 0).show(); } }); builder.show(); } /* * Multiple selection dialog */ public void showMore(View view) { Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Please choose a hobby");//Title /** * items: array of displayed data * checkedItems: Array of default checked states * listener: listen for events */ final String[] items={"football","basketball","table tennis","badminton","glass ball"}; final boolean[] checkedItems={true,false,false,false,false}; builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { //Although no logical operation is performed,,, due to the existence of this listener,,, the set default state value can be changed } }); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Traverse the state to get the selected subscript and take the value for (int i = 0; i < checkedItems.length; i++) { if (checkedItems[i]) {//selected Toast.makeText(MainActivity.this, "Hobby is:"+items[i], 0).show(); } } } }); builder.show(); } /* * Pop up a custom dialog box: customize your own layout/you must find the control through your own view object/if you want to display it, use dialog.show()/manually close the method dialog.dismiss(); */ public void showMine(View view){ //1. Create a constructor object Builder builder = new AlertDialog.Builder(MainActivity.this); //2. Create an AlertDialog object through the constructor object,,, final AlertDialog dialog = builder.create(); //3. Set your own layout (view object) through the AlertDialog object, ,, View contentView=View.inflate(MainActivity.this, R.layout.dialog_layout, null); dialog.setView(contentView); //4. Logical operations can be performed, ,, button monitoring, ,, to find controls or must pass, view objects to find Button btn = (Button) contentView.findViewById(R.id.btn); final EditText edit_text = (EditText) contentView.findViewById(R.id.edit_text); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, edit_text.getText().toString(), 0).show(); //In the custom dialog,,, you can manually close the dialog dialog.dismiss(); } }); //5. Display the dialog dialog.show(); } public void show_window(View view) { View view2=View.inflate(MainActivity.this, R.layout.activity_main, null); View contentView=View.inflate(MainActivity.this, R.layout.pop_layout, null); final PopupWindow window = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); window.setBackgroundDrawable(new BitmapDrawable()); window.setOutsideTouchable(true); window.setFocusable(true); window.setTouchable(true); Button btn1 = (Button) contentView.findViewById(R.id.btn1); final EditText edit_text1 = (EditText) contentView.findViewById(R.id.edit_text1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, edit_text1.getText().toString(), 0).show(); window.dismiss(); } }); window.showAtLocation(view2, Gravity.BOTTOM, 0, 0); } }
Basic implementation of AlertDialog
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324584508&siteId=291194637
Ranking