android几种对话框和菜单
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
Java代码 package com.example.ADemo4; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MyActivity extends Activity { public static final int RED_MENU_ID = Menu.FIRST; public static final int GREEN_MENU_ID = Menu.FIRST + 1; public static final int BLUE_MENU_ID = Menu.FIRST + 2; private Button mButton; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { (new AlertDialog.Builder(MyActivity.this)).setMessage("确定是否退出程序") .setTitle("普通对话框") .setCancelable(false) .setPositiveButton("确定退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //To change body of implemented methods use File | Settings | File Templates. MyActivity.this.finish(); } }) .setNegativeButton("取消推出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.cancel(); } }) .create() .show(); } }); Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { (new AlertDialog.Builder(MyActivity.this)) .setTitle("对话框头部") .setTitle("List对话框") .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int which) { String[] items = getResources().getStringArray(R.array.select_dialog_items); new AlertDialog.Builder(MyActivity.this) .setMessage("You selected: " + which + " , " + items[which]) .show(); } }).create() .show(); } }); Button button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { (new AlertDialog.Builder(MyActivity.this)) .setTitle("对话框头部") .setTitle("Radio对话框") .setSingleChoiceItems(R.array.select_dialog_items, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int which) { String[] items = getResources().getStringArray(R.array.select_dialog_items); new AlertDialog.Builder(MyActivity.this) .setMessage("You selected: " + which + " , " + items[which]) .show(); } }).create() .show(); } }); Button button3 = (Button) findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { (new AlertDialog.Builder(MyActivity.this)) .setTitle("对话框头部") .setTitle("Check对话框") .setMultiChoiceItems(R.array.select_dialog_items, new boolean[]{false, true, false, true}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i, boolean b) { Toast toast = Toast.makeText(MyActivity.this, i + ":" + b, Toast.LENGTH_SHORT); toast.show(); } }) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .create() .show(); } }); LayoutInflater factory = LayoutInflater.from(this); final View textEntryView = factory.inflate(R.layout.dialog, null); Button button4 = (Button) findViewById(R.id.button4); button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { (new AlertDialog.Builder(MyActivity.this)) .setTitle("对话框头部") .setTitle("继承View对话框") .setView(textEntryView) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .create() .show(); } }); } //创建菜单 @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); //To change body of overridden methods use File | Settings | File Templates. menu.add(0, RED_MENU_ID, 0, R.string.red); menu.add(0, GREEN_MENU_ID, 0, R.string.green); menu.add(0, BLUE_MENU_ID, 0, R.string.blue); return true; } //菜单事件 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case RED_MENU_ID: mButton.setBackgroundColor(Color.RED); mButton.setText(R.string.red); return true; case GREEN_MENU_ID: mButton.setBackgroundColor(Color.GREEN); mButton.setText(R.string.green); return true; case BLUE_MENU_ID: mButton.setBackgroundColor(Color.BLUE); mButton.setText(R.string.blue); return true; } return super.onOptionsItemSelected(item); } } |
