仿照Android标准API写的各种形式的弹出框
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">DialogSamples</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="two_button_choice">两个button的选择</string>
<string name="Confirm">确定</string>
<string name="Cancel">取消</string>
<string name="two_button_choice_with_long_message">两个button加上一长条消息</string>
<string name="long_message_for_two_button_choice">
Yesterday once more 往日重现
The carpenters
When 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依然闪亮
</string>
<string name="Something">发生了一些事情,我到底应该怎么样才能拯救你</string>
<string name="select_dialog">选择框</string>
<string name="alert_dialog_hide">隐藏当前窗口</string>
<string name="dialog_single_choice">单选框</string>
<string name="dialog_multi_choice">多选框</string>
<string name="dialog_multi_choice_cursor">多选光标</string>
<string name="alert_dialog_username">用户名</string>
<string name="alert_dialog_password">密码</string>
<string name="alert_dialog_text_entry">文本进入框</string>
<string name="alert_dialog_two_buttons_title">传统的两个按钮</string>
<string name="alert_dialog_two_buttons_title_holo">holo主题的按钮</string>
</resources>
arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="select_dialog_items">
<item>Command one</item>
<item>Command two</item>
<item>Command three</item>
<item>Command four</item>
</string-array>
<string-array name="select_dialog_items2">
<item>Map</item>
<item>Satellite</item>
<item>Traffic</item>
<item>Street view</item>
</string-array>
<string-array name="select_dialog_items3">
<item>Every Monday</item>
<item>Every Tuesday</item>
<item>Every Wednesday</item>
<item>Every Thursday</item>
<item>Every Friday</item>
<item>Every Saturday</item>
<item>Every Sunday</item>
</string-array>
</resources>
main.xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/two_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Two_Button" />
<Button
android:id="@+id/two_button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Two_Button_With_LONG_MESSAGE" />
<Button
android:id="@+id/select_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button_for_slection"/>
<Button
android:id="@+id/progress_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="progress_button" />
<Button
android:id="@+id/radio_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="single_choice_list" />
<Button
android:id="@+id/checkbox_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="multiple_choice" />
<Button
android:id="@+id/checkbox_button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="multiple_choice_cursor" />
<Button
android:id="@+id/text_entry_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Entry Dialog" />
<Button
android:id="@+id/two_buttons_old_school"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK Cancel dialog with traditional theme" />
<Button
android:id="@+id/two_buttons_holo_light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK Cancel dialog with Holo Light theme" />
</LinearLayout>
</ScrollView>
alert_dialog_text_entry.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/username_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="@string/alert_dialog_username"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/username_edit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/password_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="@string/alert_dialog_password"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/password_edit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:password="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="leihu.dialogsamples"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="leihu.dialogsamples.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
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);
}
}
效果图如下:
posted on 2013-08-05 12:01 leihupqrst 阅读(500) 评论(0) 收藏 举报
浙公网安备 33010602011771号