strings.xml:
DialogSamples Settings Hello world! 两个button的选择 确定 取消 两个button加上一长条消息 Yesterday once more 往日重现The carpentersWhen I was young我年轻的时候 I\'d listen to the radio常听收音机 Waiting for my favorite songs总是期待我喜爱的歌 When they played I\'d sing along我会跟随伴奏歌唱It made me smile笑声在歌声中荡漾Those were such happy time那真是幸福的时光 And not so long ago就在不久以前How I wondered where they\'d gone我还疑惑咋不见了歌But they\'re back again 像久违的朋友那样Just like a long lost friend我那么喜爱的歌All the songs I loved so well又全部回归了我的生活Every Sha-la-la-la每一个沙啦啦Every Wo-o-wo-o每一声沃沃沃Still shines依然闪亮Every shing-a-ling-a-ling每一个开唱That they\'re starting to sing\’s每一声银铃般的歌喉So fine都如此美妙When they get to the part当唱到了那一段where he\'s breaking her heart他伤透了她的心It can really make me cry这真让我潸然泪下Just like before还和从前一样 It\'s yesterday once more往日情景又重现在眼前Looking back on how it was in years gone by回首以往感叹岁月匆匆而过And the good times that I had细数我曾经美好的时光Makes today seem rather sad使得今天略显悲凉So much has changed没了往日的欢乐It was songs of love that让我耳熟能详的I would sing to then正是当年我会唱的And I\'d memorize each word一首首情歌Those old melodies那些旧旋律Still sound so good to me仍然这般地动我心魄 As they melt the years away歌中融入了逝去的年华 Every Sha-la-la-la每一个沙啦啦Every Wo-o-wo-o每一声沃沃沃Still shines依然闪亮Every shing-a-ling-a-ling每一个开唱That they\'re starting to sing\’s每一声银铃般的歌喉So fine都如此美妙All my best memories所有我最美的记忆 Come back clearly to me歌声都清晰地带回Some can even make me cry有些歌甚至能让我落泪Just like before还和从前一样It\'s yesterday once more往日情景又重现在眼前Every Sha-la-la-la每一个沙啦啦Every Wo-o-wo-o每一声沃沃沃Still shines依然闪亮Every shing-a-ling-a-ling每一个开唱 That they\'re starting to sing\’s每一声银铃般的歌喉 So fine都如此美妙 Every Sha-la-la-la每一个沙啦啦 Every Wo-o-wo-o每一声沃沃沃 Still shines依然闪亮 发生了一些事情,我到底应该怎么样才能拯救你 选择框 隐藏当前窗口 单选框 多选框 多选光标 用户名 密码 文本进入框 传统的两个按钮 holo主题的按钮
arrays.xml:
- Command one
- Command two
- Command three
- Command four
- Map
- Satellite
- Traffic
- Street view
- Every Monday
- Every Tuesday
- Every Wednesday
- Every Thursday
- Every Friday
- Every Saturday
- Every Sunday
main.xml:
alert_dialog_text_entry.xml:
AndroidManifest.xml:
MainActivity.java:
package leihu.dialogsamples;import android.os.Build;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.provider.ContactsContract;import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.database.Cursor;import android.util.Log;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity { private static final int EIGHT_ID = Menu.FIRST + 1; private static final int SIXTEEN_ID = Menu.FIRST + 2; private static final int TWENTY_FOUR_ID = Menu.FIRST + 3; private static final int TWO_ID = Menu.FIRST + 4; private static final int THIRTY_TWO_ID = Menu.FIRST + 5; private static final int ONE_ID = Menu.FIRST + 6; private static final int DIALOG_YES_NO_MESSAGE = 1; private static final int DIALOG_YES_NO_LONG_MESSAGE = 2; private static final int DIALOG_LIST = 3; private static final int DIALOG_PROGRESS = 4; private static final int DIALOG_SINGLE_CHOICE = 5; private static final int DIALOG_MULTIPLE_CHOICE = 6; private static final int DIALOG_MULTIPLE_CHOICE_CURSOR = 7; private static final int DIALOG_TEXT_ENTRY = 8; private static final int DIALOG_YES_NO_OLD_SCHOOL_MESSAGE = 9; private static final int DIALOG_YES_NO_HOLO_LIGHT_MESSAGE = 10; private ProgressDialog mProgressDialog; private int mProgress; private Handler mProgressHandler; private static final int MAX_PROGRESS = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button twoButtonTitle = (Button)findViewById(R.id.two_button); twoButtonTitle.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(DIALOG_YES_NO_MESSAGE); } }); Button twoButton2Title = (Button)findViewById(R.id.two_button2); twoButton2Title.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(DIALOG_YES_NO_LONG_MESSAGE); } }); Button selectButton = (Button)findViewById(R.id.select_button); selectButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(DIALOG_LIST); } }); Button progressButton = (Button)findViewById(R.id.progress_button); progressButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(DIALOG_PROGRESS); mProgress = 0; mProgressDialog.setProgress(0); mProgressHandler.sendEmptyMessage(0); } }); mProgressHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(mProgress >= MAX_PROGRESS){ mProgressDialog.dismiss(); } else{ mProgress++; mProgressDialog.incrementProgressBy(1); mProgressHandler.sendEmptyMessageDelayed(0, 100); } } }; Button radioButton = (Button)findViewById(R.id.radio_button); radioButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(DIALOG_SINGLE_CHOICE); } }); Button checkBox = (Button)findViewById(R.id.checkbox_button); checkBox.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(DIALOG_MULTIPLE_CHOICE); } }); Button checkBox2 = (Button)findViewById(R.id.checkbox_button2); checkBox2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(DIALOG_MULTIPLE_CHOICE_CURSOR); } }); Button textEntry = (Button)findViewById(R.id.text_entry_button); textEntry.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(DIALOG_TEXT_ENTRY); } }); Button twoButtonsOldSchoolTitle = (Button)findViewById(R.id.two_buttons_old_school); twoButtonsOldSchoolTitle.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(DIALOG_YES_NO_OLD_SCHOOL_MESSAGE); } }); Button twoButtonsHoloLightTitle = (Button)findViewById(R.id.two_buttons_holo_light); twoButtonsHoloLightTitle.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(DIALOG_YES_NO_HOLO_LIGHT_MESSAGE); } }); } //在用户第一次按下Menu键的时候将触发onCreateOptionsMenu(Menu menu) @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. menu.add(Menu.NONE, ONE_ID, 1, "1 Pixel"); menu.add(Menu.NONE, TWO_ID, 2, "2 Pixel"); menu.add(Menu.NONE, EIGHT_ID, 3, "8 Pixel"); menu.add(Menu.NONE, SIXTEEN_ID, 4, "16 Pixel"); menu.add(Menu.NONE, TWENTY_FOUR_ID, 5, "24 Pixel"); menu.add(Menu.NONE, THIRTY_TWO_ID, 6, "32 Pixel"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch(item.getItemId()){ case ONE_ID: Log.d("myDebug", "Id one is clicked!"); break; } return super.onOptionsItemSelected(item); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") @Override protected Dialog onCreateDialog(int id) { switch(id){ case DIALOG_YES_NO_MESSAGE: return new AlertDialog.Builder(MainActivity.this) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.two_button_choice) .setPositiveButton(R.string.Confirm, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Log.i("myDebug", "Your mother ask you to go home to have dinner!"); } }) .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Log.i("myDebug", "Your mother said that you don't need to go home to have dinner today!"); } }).create(); case DIALOG_YES_NO_LONG_MESSAGE: return new AlertDialog.Builder(MainActivity.this) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.two_button_choice_with_long_message) .setMessage(R.string.long_message_for_two_button_choice) .setPositiveButton(R.string.Confirm, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Log.i("myDebug", "Your mother ask you to go home to have dinner!"); } }) .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Log.i("myDebug", "Your mother said that you don't need to go home to have dinner today!"); } }) .setNeutralButton(R.string.Something, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Log.i("myDebug", "Something happened!"); } }).create(); case DIALOG_LIST: return new AlertDialog.Builder(MainActivity.this) .setTitle(R.string.select_dialog) .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String[] items = getResources().getStringArray(R.array.select_dialog_items); new AlertDialog.Builder(MainActivity.this) .setMessage("YOU selected:" + which + "," + items[which]) .show(); } }).create(); case DIALOG_PROGRESS: mProgressDialog = new ProgressDialog(MainActivity.this, ProgressDialog.THEME_TRADITIONAL); mProgressDialog.setIcon(android.R.attr.alertDialogIcon); mProgressDialog.setTitle(R.string.select_dialog); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setMax(MAX_PROGRESS); mProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE, getText(R.string.alert_dialog_hide), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getText(R.string.Cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); return mProgressDialog; case DIALOG_SINGLE_CHOICE: return new AlertDialog.Builder(MainActivity.this) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.dialog_single_choice) .setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub new AlertDialog.Builder(MainActivity.this) .setMessage("You choosed Item:" + which) .show(); } }) .setPositiveButton(R.string.Confirm, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .create(); case DIALOG_MULTIPLE_CHOICE: return new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_DARK) .setIcon(R.drawable.ic_popup_reminder) .setTitle(R.string.dialog_multi_choice) .setMultiChoiceItems(R.array.select_dialog_items, new boolean[]{false, true, false, true, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub String choice = isChecked ? "被选中" : "没有选中" ; System.out.println("第" + which + "个item" + choice); } }) .setPositiveButton(R.string.Confirm, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .create(); case DIALOG_MULTIPLE_CHOICE_CURSOR: String[] projection = new String[]{ ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.SEND_TO_VOICEMAIL }; Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, projection, null, null, null); return new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_TRADITIONAL) .setIcon(R.drawable.ic_popup_reminder) .setTitle(R.string.dialog_multi_choice_cursor) .setMultiChoiceItems(cursor, ContactsContract.Contacts.SEND_TO_VOICEMAIL, ContactsContract.Contacts.DISPLAY_NAME, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(MainActivity.this, "Readonly Demo Only - Data will not be updated", Toast.LENGTH_SHORT).show(); } }) .create(); case DIALOG_TEXT_ENTRY: LayoutInflater factory = LayoutInflater.from(this); final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null); return new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_TRADITIONAL) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_text_entry) .setView(textEntryView) .setPositiveButton(R.string.Confirm, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .create(); case DIALOG_YES_NO_OLD_SCHOOL_MESSAGE: return new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_TRADITIONAL) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_two_buttons_title) .setPositiveButton(R.string.Confirm, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .create(); case DIALOG_YES_NO_HOLO_LIGHT_MESSAGE: return new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_HOLO_LIGHT) .setIconAttribute(android.R.attr.alertDialogIcon) .setTitle(R.string.alert_dialog_two_buttons_title_holo) .setPositiveButton(R.string.Confirm, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .create(); } return super.onCreateDialog(id); }}
效果图如下: