免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4484 | 回复: 1
打印 上一主题 下一主题

自定义的Dialog对话框 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-15 09:55 |只看该作者 |倒序浏览
Android平台
Eclipse开发工具
可能不是最好的,希望大家多指正。


MainActivity部分的代码
  1. package com.example.mydialog;

  2. import android.os.Bundle;
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.app.AlertDialog;
  6. import android.content.Intent;
  7. import android.graphics.Color;
  8. import android.view.Menu;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.view.Window;
  12. import android.widget.Button;
  13. import android.widget.ImageButton;
  14. import android.widget.TextView;

  15. public class MainActivity extends Activity {

  16.     Button mBtn;
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.          
  22.         mBtn=(Button) findViewById(R.id.btn);
  23.         mBtn.setOnClickListener(new OnClickListener() {
  24.             
  25.             @Override
  26.             public void onClick(View arg0) {
  27.                 // TODO Auto-generated method stub
  28.                 showAlertDialog();
  29.             }
  30.         });
  31.          
  32.     }


  33. // 对话框
  34.     private void showAlertDialog() {

  35.         final AlertDialog dlg = new AlertDialog.Builder(this).create();
  36.         dlg.show();
  37.         Window window = dlg.getWindow();
  38.         // *** 主要就是在这里实现这种效果的.
  39.         // 设置窗口的内容页面,alertdialog.xml文件中定义view内容
  40.         window.setContentView(R.layout.alertdialog);
  41.         // 为确认按钮添加事件,执行退出应用操作
  42.         Button tv_queding = (Button) window.findViewById(R.id.tv_content1);
  43.         tv_queding.setText("  确 定   ");
  44.         tv_queding.setOnClickListener(new View.OnClickListener() {
  45.             @SuppressLint("SdCardPath")
  46.             public void onClick(View v) {

  47.                 Intent intent=new Intent();
  48.                 intent.setClass(MainActivity.this, NextActivity.class);
  49.                 startActivity(intent);
  50.                 dlg.cancel();
  51.             }
  52.         });
  53.         //取消按钮不做任何操作  可以不用添加动作
  54.         Button tv_quxiao = (Button) window.findViewById(R.id.tv_content2);
  55.         tv_quxiao.setText("  取 消   ");
  56.         tv_quxiao.setOnClickListener(new View.OnClickListener() {
  57.             public void onClick(View v) {

  58.                  

  59.                 dlg.cancel();
  60.             }
  61.         });


  62.         dlg.setCancelable(false);
  63.     }
  64.      
  65. }
复制代码
alertdialog.xml部分的代码
  1. <?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:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     android:gravity="center"
  7.     >
  8.      
  9.     <!-- 整个布局为线性布局,让最上面的图片、账户框、密码框平分上半部分,最下面的按钮独占一部分 -->
  10. <LinearLayout
  11.     android:layout_width="410dp"
  12.     android:layout_height="167dp"
  13.     android:background="@drawable/icon_bg"
  14.     android:layout_gravity="center"
  15.     android:orientation="vertical"
  16.     >
  17.      
  18.     <!-- 最上面的提示图片 -->
  19.     <ImageView
  20.         android:id="@+id/img"
  21.         android:layout_width="wrap_content"
  22.         android:layout_height="wrap_content"
  23.         android:src="@drawable/tbz198x50"
  24.         android:layout_marginTop="15dp"
  25.         android:layout_marginLeft="20dp"
  26.         android:layout_weight="1"
  27.         />
  28.      
  29.     <!-- 账号框部分 -->
  30.     <LinearLayout
  31.         android:layout_width="wrap_content"
  32.         android:layout_height="wrap_content"
  33.         android:orientation="horizontal"
  34.         android:layout_weight="1"
  35.         >
  36.          
  37.     <TextView
  38.         android:id="@+id/txt_zhanghao"
  39.         android:layout_width="wrap_content"
  40.         android:layout_height="wrap_content"
  41.         android:text="账 号:"
  42.         android:layout_marginLeft="30sp"
  43.         android:textColor="#2E9AFE"
  44.         />
  45.      
  46.     <EditText
  47.         android:id="@+id/edt_zhanghao"
  48.         android:layout_width="300dp"
  49.         android:layout_height="30dp"
  50.         android:background="@drawable/edt35x246blue"
  51.         android:layout_marginLeft="5dp"
  52.         />
  53.     </LinearLayout>
  54.      
  55.     <!-- 密码框部分 -->
  56.     <LinearLayout
  57.         android:layout_width="wrap_content"
  58.         android:layout_height="wrap_content"
  59.         android:orientation="horizontal"
  60.         android:layout_weight="1"
  61.         >
  62.          
  63.     <TextView
  64.         android:id="@+id/txt_mima"
  65.         android:layout_width="wrap_content"
  66.         android:layout_height="wrap_content"
  67.         android:text="密 码:"
  68.         android:layout_marginLeft="30sp"
  69.         android:textColor="#2E9AFE"
  70.         />
  71.     <EditText
  72.         android:id="@+id/edt_zhanghao"
  73.         android:layout_width="300dp"
  74.         android:layout_height="30dp"
  75.         android:background="@drawable/edt35x246blue"
  76.         android:password="true"
  77.         android:layout_marginLeft="5dp"
  78.         />
  79.         
  80.     </LinearLayout>
  81.     </LinearLayout>
  82.    
  83. <!-- 蓝色的线条 -->
  84.     <View
  85.         android:layout_width="410dp"
  86.         android:layout_height="1dp"
  87.         android:background="#2E9AFE"
  88.         />
  89.      
  90. <!-- 以下部分为确定和取消的两个按钮 -->
  91.     <LinearLayout
  92.         android:layout_width="410dp"
  93.         android:layout_height="60dp"
  94.         android:layout_gravity="center"
  95.         android:background="@drawable/icon_bg"
  96.         android:orientation="horizontal"
  97.         android:paddingLeft="5dp"
  98.         android:paddingRight="5dp" >

  99.         <!-- 另一个颜色值 #a8bbc6-->
  100.         <View
  101.             android:layout_width="1dp"
  102.             android:layout_height="55dp"
  103.             android:background="#fff"
  104.             android:layout_weight="1"
  105.             />
  106.         <Button
  107.             android:id="@+id/tv_content1"
  108.             android:layout_width="wrap_content"
  109.             android:layout_height="wrap_content"
  110.             android:gravity="center"
  111.             android:textColor="#2E9AFE"
  112.             android:textSize="22sp"
  113.             android:layout_gravity="center"
  114.             android:layout_weight="1"
  115.             android:background="@drawable/icon_bg"
  116.             />

  117.          <View
  118.             android:layout_width="1dp"
  119.             android:layout_height="60dp"
  120.             android:background="#fff"
  121.             android:layout_weight="1"
  122.             />
  123.         <View
  124.             android:layout_width="1dp"
  125.             android:layout_height="60dp"
  126.             android:background="#2E9AFE"
  127.             />
  128.          
  129.         <View
  130.             android:layout_width="1dp"
  131.             android:layout_height="60dp"
  132.             android:background="#fff"
  133.             android:layout_weight="1"
  134.             />

  135.             <Button
  136.                 android:id="@+id/tv_content2"
  137.                 android:layout_width="wrap_content"
  138.                 android:layout_height="wrap_content"
  139.                 android:gravity="center"
  140.                 android:textColor="#2E9AFE"
  141.                 android:textSize="22sp"
  142.                 android:layout_weight="1"
  143.                 android:layout_gravity="center"
  144.                 android:background="@drawable/icon_bg"
  145.                  />
  146.             
  147.             <View
  148.             android:layout_width="1dp"
  149.             android:layout_height="55dp"
  150.             android:background="#fff"
  151.             android:layout_weight="1"
  152.             />
  153.         </LinearLayout>
  154.          
  155.     </LinearLayout>
复制代码

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
2 [报告]
发表于 2015-07-15 11:22 |只看该作者
现在很多都是使用 DialogFragment
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP