Android自定义AlterDialog,并给其中的控件添加监听事件

来源:互联网 发布:js移动端手指滑动事件 编辑:程序博客网 时间:2024/05/17 05:04

由于Android系统中的AlterDialog界面不是很好看,而且整体颜色为灰黑,有可能和你做的项目不太搭调,在使用AlterDialog的时候,许多人都想使用自定义的的布局,这样灵活性高,布局也会比较好看,但是要给自定义布局中的控件添加监听事件,好像就不是那么一回事了,今天我就为大家写一个小小的demo,有不足之处还希望大家能够积极指出,在此感激不尽。

res/layout/activity_main.xml

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button   
  8.         android:layout_gravity="center"  
  9.         android:id="@+id/btn"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="显示弹出框"  
  13.         android:background="@drawable/button_border_radius"/>  
  14.   
  15. </LinearLayout>  

上面的背景是自定义的一个带圆角边框的按钮背景

res/drwable/button_border_radius.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <corners android:radius="30.0px" />  
  5.   
  6.     <solid android:color="#fc3d84" />  
  7.   
  8. </shape>  
[html] view plaincopy
  1. </pre><pre name="code" class="html">在定义一个弹出框的布局  
[html] view plaincopy
  1. res/layout/alter_dialog_layout.xml  
[html] view plaincopy
  1. </pre><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:paddingTop="20dp"  
  5.     android:paddingBottom="10dp"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <TextView  
  10.         android:layout_marginTop="10dp"  
  11.         android:id="@+id/alter_dialog_message"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:gravity="center"  
  15.         android:padding="5dp"  
  16.         android:text="亲爱哒,这个相册的照片很漂亮哒,是不是真的要删除呢?"  
  17.         android:textSize="16sp" />  
  18.   
  19.     <LinearLayout  
  20.         android:layout_marginTop="20dp"  
  21.         android:id="@+id/alter_dialog_button"  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:gravity="center"  
  25.         android:orientation="horizontal" >  
  26.   
  27.         <TextView  
  28.             android:id="@+id/alter_dialog_button_ensure"  
  29.             android:layout_width="wrap_content"  
  30.             android:layout_height="wrap_content"  
  31.             android:background="@drawable/button_border_radius"  
  32.             android:clickable="true"  
  33.             android:paddingBottom="3dp"  
  34.             android:paddingLeft="20dp"  
  35.             android:paddingRight="20dp"  
  36.             android:paddingTop="3dp"  
  37.             android:text="狠心删除"  
  38.             android:textColor="@android:color/white" />  
  39.   
  40.         <TextView  
  41.             android:id="@+id/alter_dialog_button_cancel"  
  42.             android:layout_width="wrap_content"  
  43.             android:layout_height="wrap_content"  
  44.             android:layout_marginLeft="50dp"  
  45.             android:background="@drawable/button_border_radius"  
  46.             android:clickable="true"  
  47.             android:paddingBottom="3dp"  
  48.             android:paddingLeft="20dp"  
  49.             android:paddingRight="20dp"  
  50.             android:paddingTop="3dp"  
  51.             android:text="再看看"  
  52.             android:textColor="@android:color/white" />  
  53.     </LinearLayout>  
  54.   
  55. </LinearLayout>  

基本的布局已经定义好了,现在关键的activity来了

src/MainActivity

[java] view plaincopy
  1. package com.example.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     private AlertDialog dialog;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         Button btn = (Button) findViewById(R.id.btn);  
  20.         btn.setOnClickListener(new OnClickListener() {  
  21.               
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 showDialog();  
  25.             }  
  26.         });  
  27.            
  28.     }  
  29.     /** 
  30.      * 显示dialog 
  31.      */  
  32.     private void showDialog(){  
  33.         LayoutInflater inflater = LayoutInflater.from(this);  
  34.         View view = inflater.inflate(R.layout.alter_dialog_layout, null);  
  35.         AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  36.         builder.setView(view);  
  37.         TextView tv = (TextView) view.findViewById(R.id.alter_dialog_message);  
  38.         tv.setText("  亲爱哒,你确定要删除这个相册吗?");  
  39.         TextView ensure = (TextView) view.findViewById(R.id.alter_dialog_button_ensure);  
  40.         TextView cancel = (TextView) view.findViewById(R.id.alter_dialog_button_cancel);  
  41.         ensure.setOnClickListener(new OnClickListener() {  
  42.               
  43.             @Override  
  44.             public void onClick(View v) {  
  45.                 //这里执行相应的删除代码  
  46.                 dialog.dismiss();  
  47.             }  
  48.         });  
  49.         cancel.setOnClickListener(new OnClickListener() {  
  50.               
  51.             @Override  
  52.             public void onClick(View v) {  
  53.                 dialog.dismiss();  
  54.             }  
  55.         });  
  56.         dialog = builder.create();  
  57.         dialog.setView(view, 0000);  
  58.         dialog.show();  
  59.     }  
  60.   
  61.   
  62. }  

0 0
原创粉丝点击