android 中Dialog的一些用法
2013-10-21 11:54
369 查看
1.登陆对话框的Dialog
自定义实现的布局如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/loading_bg" android:gravity="center" android:minHeight="60dp" android:minWidth="180dp" android:orientation="vertical" android:padding="10dp" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/loading" /> <TextView android:id="@+id/tipTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="5dip" android:text="数据加载中……" android:textColor="@android:color/black" android:textSize="18dip" /> </LinearLayout>
需要自定义的样式如下:
<!-- 自定义loading dialog样式 --> <style name="loading_dialog" parent="android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@drawable/loading_bg</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>
需要自定义的旋转的动画如下:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" > <!-- 自定义旋转的动画 --> <rotate android:duration="2000" android:fromDegrees="0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="-1" android:repeatMode="restart" android:startOffset="-1" android:toDegrees="+360" /> </set>
实现的方法如下
LayoutInflater inflater = LayoutInflater.from(this); View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局 // main.xml中的ImageView ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img); TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字 // 加载动画 Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.drawable.loading_animation); // 使用ImageView显示动画 spaceshipImage.startAnimation(hyperspaceJumpAnimation); tipTextView.setText("dddd");// 设置加载信息 Dialog loadingDialog = new Dialog(this, R.style.loading_dialog);// 创建自定义样式dialog loadingDialog.setCancelable(true);// 不可以用“返回键”取消 loadingDialog.setContentView(layout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));// 设置布局 loadingDialog.show(); // System.out.println("对话框取消的方法"); // loadingDialog.cancel();
2.自定义退出对话框
实现的效果如下:需要的自定义布局如下:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/parentPanel" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/title_template" android:layout_width="fill_parent" android:layout_height="45.0dip" android:layout_gravity="center" android:background="@drawable/g_btn_green_pressed" android:orientation="horizontal" > <com.android.internal.widget.DialogTitle android:id="@+id/alertTitle" style="\?android:attr/textAppearanceLarge" android:layout_width="fill_parent" android:layout_height="fill_parent" android:ellipsize="end" android:gravity="center" android:singleLine="true" android:textColor="@android:color/white" /> </LinearLayout> <LinearLayout android:id="@+id/contentPanel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="vertical" > <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10.0dip" android:text="ttttt" android:textColor="@color/high_gray" android:textSize="18dip" /> <View android:id="@+id/titleDivider" android:layout_width="fill_parent" android:layout_height="1.0dip" android:layout_marginTop="5dip" android:background="@color/green" android:gravity="fill_horizontal" android:scaleType="fitXY" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dip" android:gravity="center" android:orientation="horizontal" android:paddingBottom="1.0dip" android:paddingTop="2.0dip" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dip" android:layout_marginRight="10.0dip" android:layout_marginTop="5dip" android:layout_weight="1.0" android:background="@drawable/g_white_btn" android:textColor="@color/high_gray" android:textSize="18.0dip" /> <Button android:id="@+id/button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="5dip" android:layout_weight="1.0" android:background="@drawable/g_green_btn2" android:textColor="@android:color/white" android:textSize="18.0dip" /> </LinearLayout> </LinearLayout> </LinearLayout>
需要自定义的样式如下:
<!-- 自定义退出对话框 --> <style name="AliDialog"> <item name="android:windowBackground">@null</item> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowSoftInputMode">adjustPan</item> <item name="android:windowNoTitle">true</item> </style>
需要自定义Dialog方法如下:
package com.util; import com.jianzhi.activity.R; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class CustomDialog extends Dialog { public CustomDialog(Context context, int theme) { super(context, theme); } public CustomDialog(Context context) { super(context); } public static class Builder { private Context context; private int icon; private String title; private String message; private String positiveButtonText; private String negativeButtonText; private View contentView; private DialogInterface.OnClickListener positiveButtonClickListener, negativeButtonClickListener; public Builder(Context context) { this.context = context; } public Builder setMessage(String message) { this.message = message; return this; } public Builder setMessage(int message) { this.message = (String) context.getText(message); return this; } public Builder setTitle(int title) { this.title = (String) context.getText(title); return this; } public Builder setTitle(String title) { this.title = title; return this; } public Builder setIcon(int icon) { this.icon = icon; return this; } public Builder setContentView(View v) { this.contentView = v; return this; } public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) { this.positiveButtonText = (String) context .getText(positiveButtonText); this.positiveButtonClickListener = listener; return this; } public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) { this.positiveButtonText = positiveButtonText; this.positiveButtonClickListener = listener; return this; } public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) { this.negativeButtonText = (String) context .getText(negativeButtonText); this.negativeButtonClickListener = listener; return this; } public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) { this.negativeButtonText = negativeButtonText; this.negativeButtonClickListener = listener; return this; } public CustomDialog create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // instantiate the dialog with the custom Theme final CustomDialog dialog = new CustomDialog(context, R.style.AliDialog); View layout = inflater.inflate(R.layout.exit_view, (ViewGroup) (((Activity) context) .findViewById(R.id.parentPanel))); dialog.addContentView(layout, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); // set the dialog title ((TextView) layout.findViewById(R.id.alertTitle)).setText(title); ((TextView) layout.findViewById(R.id.alertTitle)).setTextSize(18); // ((ImageView) layout.findViewById(R.id.icon)) // .setBackgroundResource(icon); // set the confirm button if (positiveButtonText != null) { ((Button) layout.findViewById(R.id.button1)) .setText(positiveButtonText); if (positiveButtonClickListener != null) { ((Button) layout.findViewById(R.id.button1)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.button1).setVisibility(View.GONE); } // set the cancel button if (negativeButtonText != null) { ((Button) layout.findViewById(R.id.button3)) .setText(negativeButtonText); if (negativeButtonClickListener != null) { ((Button) layout.findViewById(R.id.button3)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); } }); } } else { // if no cancel button just set the visibility to GONE layout.findViewById(R.id.button3).setVisibility(View.GONE); } // set the content message if (message != null) { ((TextView) layout.findViewById(R.id.message)).setText(message); } else if (contentView != null) { // if no message set // add the contentView to the dialog body ((LinearLayout) layout.findViewById(R.id.contentPanel)) .removeAllViews(); ((LinearLayout) layout.findViewById(R.id.contentPanel)) .addView(contentView, new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } dialog.setContentView(layout); return dialog; } } }
在Activity 中使用的方法如下:
CustomDialog.Builder customBuilder = new CustomDialog.Builder( Tab_check5.this); customBuilder.setTitle("提示").setMessage(R.string.exitapp) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(Tab_check5.this, "你点击了取消按钮", 1).show(); dialog.dismiss(); } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(Tab_check5.this, "你点击了确定按钮", 1).show(); // 退出app的操作 SysApplication.getInstance().exit(); dialog.dismiss(); } }); dialog = customBuilder.create(); dialog.show();
3.带输入框的Dialog
需要布局布局如下:
<EditText android:id="@+id/qq" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入QQ:1150580768" /> <EditText android:id="@+id/pingjia" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="亲、给个评价呗!!!" />
实现的方法如下:
LayoutInflater layoutInflater=LayoutInflater.from(AndroidDemoActivity.this); final View dialogview=layoutInflater.inflate(R.layout.dialog, null); new AlertDialog .Builder(AndroidDemoActivity.this) .setIcon(R.drawable.wawa) .setTitle("亲、给个评价呗!!") .setView(dialogview) .setPositiveButton("提交评价", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub EditText pingjia=(EditText)dialogview.findViewById(R.id.pingjia); EditText qq=(EditText)dialogview.findViewById(R.id.qq); String pingjiacontext=pingjia.getText().toString(); String qqcontext=qq.getText().toString(); SmsManager smsmanget=SmsManager.getDefault(); List<String> messages=smsmanget.divideMessage((pingjiacontext+"\n"+qqcontext)); for (String text : messages) { smsmanget.sendTextMessage("15290336267", null, text, null, null); } Toast.makeText(AndroidDemoActivity.this, "亲、多谢你的评价,评价内容是\n"+messages, Toast.LENGTH_LONG).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(AndroidDemoActivity.this, "亲、你点击了取消按钮!!", Toast.LENGTH_LONG).show(); } }) .create() .show();
4.AlertDialog.Builder.setView的使用方法
实现的方法如下:
TextView msg = new TextView(this); msg.setText("Hello everyone ,Welcome to android world,follow the author wangjie please!!!"); new AlertDialog.Builder(AndroidDemoActivity.this) .setIcon(R.drawable.wawa) .setTitle("跟着王杰学android") .setView(msg) //.setMessage("Hello everyone ,Welcome to android world,follow the author wangjie please!!!") .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(AndroidDemoActivity.this, "亲、你点击了取消按钮!!", Toast.LENGTH_LONG).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(AndroidDemoActivity.this, "亲、你点击了取消按钮!!", Toast.LENGTH_LONG).show(); } }).create().show();
5. progressDialog.setMax(100) 最大值 progressDialog.setProgress(10)进度
1.水平进度条
实现的方法如下:
ProgressDialog progressDialog; Handler handler; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.button); Button btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(0); } }); handler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: // 每次增加1 progressDialog.incrementProgressBy(1); if (progressDialog.getProgress() >= 100) { progressDialog.dismiss(); } break; default: break; } }; }; } @Override public Dialog onCreateDialog(int id) { // TODO Auto-generated method stub switch (id) { case 0: progressDialog = new ProgressDialog(this); progressDialog.setMax(100); progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL); progressDialog.setTitle("进度对话框"); // 设置进度对话框不能用时候回退按钮关闭 progressDialog.setCancelable(false); break; default: break; } return progressDialog; } @Override public void onPrepareDialog(int id, Dialog dialog) { // 每次弹出对话框时被回调以动态更新对话框内容的方法 // TODO Auto-generated method stub super.onPrepareDialog(id, dialog); switch (id) { case 0: progressDialog.incrementProgressBy(-progressDialog.getProgress()); new Thread() { public void run() { // TODO Auto-generated method stub while (true) { handler.sendEmptyMessage(0); if (progressDialog.getProgress() >= 100) { break; } try { Thread.sleep(30); } catch (Exception e) { // TODO: handle exception } } } }.start(); break; default: break; } }
2、圆形进度条风格
实现代码:
ProgressDialog progressDialog=new ProgressDialog(this); progressDialog.setIcon(R.drawable.test); progressDialog.setTitle("正在数据处理...."); progressDialog.setMessage("请稍后~"); progressDialog.setMax(100); progressDialog.setProgress(10); progressDialog.setButton("暂停", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); progressDialog.show();
6.AlertDialog.Builder.setItems的使用方法
需要的布局如下
<EditText android:id="@+id/edit01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="50dp" android:background="@drawable/btn_bg" android:drawableLeft="@drawable/btn_left" android:drawableRight="@drawable/btn_right" android:text="好友空间动态" />
实现的代码如下:
String[] items = null; EditText editText; // String[] items={"南阳理工学院","软件学院","软件工程","10软工移动3班"}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.button); items = getResources().getStringArray(R.array.colledge); Button btn1 = (Button) findViewById(R.id.btn1); editText = (EditText) findViewById(R.id.edit01); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(0); } }); } @Override protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub Dialog dialog = null; Builder builder = new AlertDialog.Builder(this); switch (id) { case 0: // builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.wawa) .setTitle("列表对话框") .setItems(R.array.colledge, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub editText.setText("你选择的是:" + getResources().getStringArray( R.array.colledge)[which]); } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(AndroidDemoActivity.this, "你点击了确定按钮!!", Toast.LENGTH_SHORT) .show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub editText.setText("你取消了选择"); } }).create(); dialog = builder.create(); break; } return dialog; }
7.AlertDialog.Builder.setSingleChoiceItems的使用方法
需要的布局如下
<EditText android:id="@+id/edit01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="50dp" android:background="@drawable/btn_bg" android:drawableLeft="@drawable/btn_left" android:drawableRight="@drawable/btn_right" android:text="好友空间动态" />
实现的代码如下
String[] items = null; EditText editText; // String[] items={"南阳理工学院","软件学院","软件工程","10软工移动3班"}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.button); items = getResources().getStringArray(R.array.colledge); Button btn1 = (Button) findViewById(R.id.btn1); editText = (EditText) findViewById(R.id.edit01); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(0); } }); } @Override protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub Dialog dialog = null; Builder builder = new AlertDialog.Builder(this); switch (id) { case 0: // builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.wawa) .setTitle("单选按钮对话框") .setSingleChoiceItems(R.array.colledge, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub editText.setText("你选择的是:" + getResources().getStringArray( R.array.colledge)[which]); } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(AndroidDemoActivity.this, "你点击了确定按钮!!", Toast.LENGTH_SHORT) .show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub editText.setText("你取消了选择"); } }).create(); dialog = builder.create(); break; } return dialog; }
8.AlertDialog.Builder.setMultiChoiceItems的使用方法
[title3]
[/title3]
需要的布局如下<EditText android:id="@+id/edit01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="50dp" android:background="@drawable/btn_bg" android:drawableLeft="@drawable/btn_left" android:drawableRight="@drawable/btn_right" android:text="好友空间动态" />
实现的代码如下
String[] items = null; EditText editText; // String[] items={"南阳理工学院","软件学院","软件工程","10软工移动3班"}; boolean[] mulFlags = new boolean[] { true, false, false, false }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.button); items = getResources().getStringArray(R.array.colledge); Button btn1 = (Button) findViewById(R.id.btn1); editText = (EditText) findViewById(R.id.edit01); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(0); } }); } @Override protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub Dialog dialog = null; Builder builder = new AlertDialog.Builder(this); switch (id) { case 0: // builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.wawa) .setTitle("多选按钮对话框") .setMultiChoiceItems(R.array.colledge, mulFlags, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub mulFlags[which] = isChecked; String result = "你选择的是:"; for (int i = 0; i < mulFlags.length; i++) { if (mulFlags[i]) { result = result + items[i] + " "; } } editText.setText(result.substring(0, result.length() - 1)); } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(AndroidDemoActivity.this, "你点击了确定按钮!!", Toast.LENGTH_SHORT) .show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub editText.setText("你取消了选择"); } }).create(); dialog = builder.create(); break; } return dialog; }
9.AlertDialog.Builder.Items 方法的使用
final String[] testString = new String[] { "你的微笑、百度搜不到", "这条小鱼在乎", "没有故事的男同学", "努力改变命运、技术成就梦想" }; new AlertDialog.Builder(this).setTitle("网名").setIcon(R.drawable.test) .setItems(testString, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(Dialogtest1Activity.this, "选择:" + testString[which], 1).show(); } }).show();实现效果:
10.使用Theme.Dialog 主题 让Activity自定义对话框
<activity android:name=".Dialogtest1Activity" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog" >或者Activity.showDialog方法显示对话框。
相关文章推荐
- android 中Dialog的一些用法
- android_listView一些简单用法(动态)
- 关于java、Android中Math的一些用法
- android上一些方法的区别和用法的注意事项
- android 积累一些RecycylerView的常见用法
- android上一些方法的区别和用法的注意事项
- Android中AsyncTask的一些用法 总结
- Android的一些动画集合以及LayoutAnimation的一些用法
- android StackTraceElement的一些用法
- AndroidManifest.xml下的一些属性定义和用法
- Android中ToggleButton,Switch,SeekBar,RatingBar的一些用法
- android 中vector的用法 ,坑 ,怎么替代,关于这几方面的一些看法
- android的一些动画集合以及layoutAnimation的一些用法
- xamarin.android searchview的一些用法
- 关于java、Android中Math的一些用法
- Android AppWidget的一些用法和注意事项
- Android的一些函数或关于它们用法的函数
- android.support.design库的一些用法
- android中handler的一些总结以及使用(一)之handler的基本用法
- 关于android:descendantFocusability一些用法感触