Android 之自定义弹出提示框
来源:互联网 发布:blog与mysql的分离 编辑:程序博客网 时间:2024/05/29 18:13
第一步:编写MyDialog继承Dialog
public class MyDialog extends Dialog {
private TextView title;
private TextView message;
private TextView no;
private TextView yes;
public MyDialog(Context context) {
super(context);
InitView();
}
public MyDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
InitView();
}
public MyDialog(Context context, int theme) {
super(context, theme);
InitView();
}
private void InitView() {
setContentView(R.layout.dialog);
//实例化控件
title=(TextView) findViewById(R.id.title);
message=(TextView) findViewById(R.id.message);
no=(TextView) findViewById(R.id.no);
yes=(TextView) findViewById(R.id.yes);
}
//对外提供内容信息和标题信息的方法
public void setInfo(String title,String message){
this.title.setText(title);
this.message.setText(message);
}
//对外提供按钮监听的接口方法
public void setListenerNo(View.OnClickListener noListener){
no.setOnClickListener(noListener);
}
public void setListenerYes(View.OnClickListener yesListener){
yes.setOnClickListener(yesListener);
}
}
第二步:看看dialog现实的布局dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/dialog_bg"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:gravity="center"
android:layout_width="match_parent"
android:text="标题:今天你好吗?"
android:textSize="20dp"
android:textColor="#CCCCCC"
android:layout_height="40dp"/>
<TextView
android:id="@+id/message"
android:gravity="center"
android:layout_width="match_parent"
android:textColor="#CCCCCC"
android:text="内容:每天心情不好"
android:textSize="18dp"
android:layout_height="30dp"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#EEA2AD"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView
android:id="@+id/no"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="20dp"
android:textColor="#CCCCCC"
android:background="@drawable/but_bg"
android:layout_height="match_parent"
android:text="取消"/>
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#EEA2AD"/>
<TextView
android:id="@+id/yes"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="20dp"
android:background="@drawable/but_bg"
android:textColor="#CCCCCC"
android:layout_height="match_parent"
android:text="确定"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
接下来看看怎个dialog 弹出来四周为圆角的效果定义和按钮点击时效果的定义
dialog_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:radius="5dp"/>
<gradient
android:startColor="#FFFFFF"
android:endColor="#FFFFFF"/>
</shape>
but_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- //选中的颜色 -->
<item android:state_pressed="true">
<shape>
<solid android:color="#FFB5C5" />
</shape>
</item>
<!-- //默认的颜色 -->
<item android:state_pressed="false">
<shape>
<solid android:color="#00000000" />
</shape>
</item>
</selector>
接下来给dialog的style的样式
<style name="MyDialogStyle">
<item name="android:windowBackground">@android:color/transparent</item> <!-- 设置dialog的背景,此处为系统给定的透明值 -->
<item name="android:windowFrame">@null</item> <!-- Dialog的windowFrame框为无 -->
<item name="android:windowNoTitle">true</item> <!-- 是否显示标题 -->
<item name="android:windowIsFloating">true</item> <!-- 是否浮现在activity之上 -->
<item name="android:windowIsTranslucent">true</item> <!-- 是否半透明 -->
<item name="android:windowContentOverlay">@null</item> <!-- 是否有覆盖 -->
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <!-- 设置Activity出现方式 -->
<item name="android:backgroundDimEnabled">true</item> <!-- 背景是否模糊显示 -->
</style>
接下来就看是怎么调用的了,很简单:
public class MainActivity extends Activity {
private TextView tv_show;
private MyDialog myDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_show=(TextView) findViewById(R.id.tv_show);
tv_show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myDialog = new MyDialog(MainActivity.this, R.style.MyDialogStyle);
//这个方法是调用dialog中现实的文本内容
//myDialog.setInfo("哎呀", "不错嘛");
//事件监听的方法
myDialog.setListenerNo(new OnClickListener() {
@Override
public void onClick(View v) {
myDialog.dismiss();
}
});
myDialog.setListenerYes(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"好痛", 1).show();
}
});
myDialog.show();
}
});
}
}
最终我们来看看效果图吧:
- Android 之自定义弹出提示框
- android 自定义Dialog弹出提示框
- 弹出自定义提示框
- 自定义弹出提示框
- Android 开发之 Toast (弹出提示框)
- Android 搭建项目之更新提示--弹出更新提示框
- Android 弹出提示框
- android 弹出提示框
- Android弹出提示框
- android 弹出提示框
- Android自定义Toast弹出消息提示
- Android自定义弹出框
- Android 自定义弹出框
- Android之自定义Toast提示框样式
- Android中的弹出提示框
- Android中的弹出提示框
- Android中的弹出提示框
- javascrpt自定义弹出提示框,仅具备提示功能
- 大华乐橙云JavaWEB版实现网页直播远程监控DEMO
- vim常用操作命令 合集
- Ruby元编程-Week-1
- 怎么给MFC弹出右键选择框的效果,Cmenu
- 【网络流】:一些基本知识
- Android 之自定义弹出提示框
- Hive内存溢出的问题
- Struts2中的OGNL详解
- 关于iOS中如何给字体加粗的问题总结
- Spring MVC和Spring配置AOP
- android 禁止scrollview 因控件变化自动滚动到底的方法
- iOS使用自定义字体
- 偏光片在LCD中的应用
- getParameter 与 getAttribute的区别