zoukankan      html  css  js  c++  java
  • Android自定义Dialog

    原文地址:http://www.cnblogs.com/sense7/archive/2012/06/05/2536750.html

    最近公司没什么项目做,大部分时间都是自己在学习,而且觉得有必要和各位园友分享、交流下自己的所学所得,所以呢,决定今天开始写博吧。

    嗯嗯,步入正题,很多时候Android自带的控件样式不能满足我们多样化的需求,要自己去自定义才会给人耳目一新的感觉,今天就先拿AlertDialog开导,哈~先上效果图(比较喜欢柯南O(∩_∩)O):

    点击enter按钮会关闭对话框,留在当前Activity,点击exit按钮则退出应用。

    首先是main.xml:

    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFFFF"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
    
    </LinearLayout>
    复制代码

    主Activity代码CustomAlertDialogActivity.java:

    复制代码
    package nbe.sense7.vinci.custom.alertdialog;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.widget.Button;
    import android.widget.ImageButton;
    
    public class CustomAlertDialogActivity extends Activity {
        /** Called when the activity is first created. */
        private Button button;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            //点击弹出自定义对话框
            button = (Button)findViewById(R.id.button);
            button.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    showCustomAlertDialog();
                }
            });
        }
        
        private void showCustomAlertDialog(){
            final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.show();
            Window win = alertDialog.getWindow();
            //设置自定义的对话框布局
            win.setContentView(R.layout.custom_alertdialog);
            
            //关闭对话框按钮事件
            ImageButton enterBtn = (ImageButton)win.findViewById(R.id.enter_btn);
            enterBtn.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    alertDialog.cancel();
                }
            });
            
            //退出程序
            ImageButton exitBtn = (ImageButton)win.findViewById(R.id.exit_btn);
            exitBtn.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    CustomAlertDialogActivity.this.finish();
                }
            });
        }
    }
    复制代码

    自定义对话框布局文件custom_alertdialog.xml:

    复制代码
    <?xml version="1.0" encoding="UTF-8" ?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="15dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:background="@drawable/dialog_bg">
        
        <!-- enter button -->
        <ImageButton 
            android:id="@+id/enter_btn"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_margin="15dp"
            android:layout_gravity="bottom"
            android:src="@drawable/enter_btn"/>
        
        <!-- quit button -->
        <ImageButton 
            android:id="@+id/exit_btn"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_margin="15dp"
            android:layout_gravity="bottom"
            android:src="@drawable/exit_btn"/>
        
    </LinearLayout>
    复制代码
     
     关键代码:
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.show();
            Window win = alertDialog.getWindow();
            //设置自定义的对话框布局
            win.setContentView(R.layout.custom_alertdialog);
       
            ImageButton enterBtn = (ImageButton)win.findViewById(R.id.enter_btn);
            enterBtn.setOnClickListener(new OnClickListener(){});
            
          
            ImageButton exitBtn = (ImageButton)win.findViewById(R.id.exit_btn);
            exitBtn.setOnClickListener(new OnClickListener(){});
        }
    当然如果只是做一个简单的 dialog样式的话,这么做起来还是挺方便的,但是如果要实现复杂的功能和效果,还是要继承android.app.Dialog来实现。
  • 相关阅读:
    eg_5
    浅谈Java中的Hashmap
    java中方法传入参数时:值传递还是址传递?
    重温概率学(一)期望、均值、标准差、方差
    博客搬家
    golang sync/atomic
    单机配置kafka和zookeeper
    异步消息队列组件
    2017总结
    看完轻松年薪30w+
  • 原文地址:https://www.cnblogs.com/oasis2008/p/2557982.html
Copyright © 2011-2022 走看看