Android继承对话框Dialog

       初次进行安卓开发,水平有限,主要是发挥搜索的作用。

 

       1.首先搞一个xml布局文件,名字自定义,我就直接叫做Mydialog.xml,这是最后弹出的框,直接上代码,上图 。    注意:红色标注的地方都要自己修改。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@+id/自定义id"
 4     android:layout_width="fill_parent"
 5     android:layout_height="wrap_content"
 6     android:background="@drawable/自定义背景样式"
 7     android:minWidth="200dp"
 8     android:orientation="vertical"
 9     android:padding="10dp"
10     android:paddingBottom="30dp"
11     android:paddingTop="30dp"
12     android:layout_marginTop="250dp"
13     >
14 
15     <EditText
16 
17         android:id="@+id/自定义id"
18         android:layout_width="fill_parent"
19         android:layout_height="wrap_content"
20         android:background="@drawable/自定义背景样式"
21         android:hint="编辑框1"
22         android:minHeight="45dp"
23         android:textSize="18sp" />
24 
25     <EditText
26         android:id="@+id/自定义id"
27         android:layout_width="fill_parent"
28         android:layout_height="wrap_content"
29         android:layout_marginTop="5dp"
30         android:background="@drawable/自定义背景样式"
31         android:hint="编辑框2"
32         android:minHeight="45dp"
33         android:textSize="18sp" />
34 
35     <EditText
36         android:id="@+id/自定义id"
37         android:layout_width="fill_parent"
38         android:layout_height="wrap_content"
39         android:layout_marginTop="5dp"
40         android:background="@drawable/自定义背景样式"
41         android:gravity="top|left"
42         android:hint="编辑框3"
43         android:minHeight="145dp"
44         android:textSize="18sp" />
45 
46     <Button
47         android:id="@+id/自定义id"
48         android:layout_width="fill_parent"
49         android:layout_height="wrap_content"
50         android:layout_marginTop="5dp"
51         android:background="@drawable/自定义背景样式"
52         android:text="按钮" />
53 
54 </LinearLayout>

 

       2.其次,搞一个类继承于Dialog--我就直接叫做MyDialog了,我用的是最近比较火的kotlin语言。注意:红色标注的地方都要自己修改。

         

 1 import android.app.Activity
 2 import android.app.Dialog
 3 import android.os.Bundle
 4 import android.view.Display
 5 import android.view.View
 6 import android.view.Window
 7 import android.view.WindowManager
 8 import android.widget.Button
 9 import android.widget.EditText
10 
11 这里要导入项目的包
12 这里要导入R的路径
13 
14 /**
15  * Created by Jason_Jan on 2017/7/2.
16  */
17 
18 class MyDialog : Dialog {
19 
20     internal var context: Activity
21 
22     private var btn_save: Button? = null
23 
24     var text_name: EditText?=null
25 
26     var text_mobile: EditText?=null
27 
28     var text_info: EditText?=null
29 
30 
31     private var mClickListener: View.OnClickListener?=null
32 
33     constructor(context: Activity) : super(context) {
34         this.context = context
35     }
36 
37     constructor(context: Activity, theme: Int, clickListener: View.OnClickListener) : super(context, theme) {
38         this.context = context
39         this.mClickListener = clickListener
40     }
41 
42     override fun onCreate(savedInstanceState: Bundle) {
43         super.onCreate(savedInstanceState)
44         // 指定布局
45         this.setContentView(R.layout.自定义弹框的布局)
46 
47         text_name = findViewById(R.id.弹框布局中拿到视图id) as EditText
48         text_mobile = findViewById(R.id.弹框布局中拿到视图id) as EditText
49         text_info = findViewById(R.id.弹框布局中拿到视图id) as EditText
50 
51         /*
52          * 获取窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getWindow(),表示获得这个Activity的Window
53          * 对象,这样这可以以同样的方式改变这个Activity的属性.
54          */
55         val dialogWindow = this.window
56 
57         val m = context.windowManager
58         val d = m.defaultDisplay // 获取屏幕宽、高用
59         val p = dialogWindow!!.attributes // 获取对话框当前的参数值
60         // p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
61         p.width = (d.width * 0.8).toInt() // 宽度设置为屏幕的0.8
62         dialogWindow.attributes = p
63 
64         // 根据id在布局中找到控件对象
65         btn_save = findViewById(R.id.从布局中拿到的id) as Button
66 
67         // 为按钮绑定点击事件监听器
68         btn_save!!.setOnClickListener(mClickListener)
69 
70         this.setCancelable(true)
71     }
72
73 }

 

 

          3.再随便搞一个Activity,对应一个布局,这个布局简单放一个按钮,用来做弹出框的背景。上代码,上图。注意:红色部分需要修改。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="自己的包名">

    <Button
        android:layout_width="300dp"
        android:layout_height="wrap_content"

        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text="测试弹出框"
android:onClick="对应Activity的点击事件" android:background="@drawable/自定义背景样式" /> </RelativeLayout>

 

  4.最后用新建的Activity来设置button的点击事件。注意:红色地方需要更改。

 

导入项目的包
导入R的路径 import android.app.Activity import android.os.Bundle import android.view.View class MydialogTest : Activity() { var myDialog:自定义弹出框类?=null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.自定义测试布局) } fun showDialog(view: View) { myDialog= 自定义弹出框类(this, 自定义测试布局或者系统布局都可以, onClickListener)//第三个参数是监听事件 myDialog?.show() } val onClickListener = object : View.OnClickListener {//写这个监听事件 override fun onClick(v: View?) { when (v?.getId()) { R.id.从布局获取的按钮id -> { val name = myDialog?.布局中的一个text名字1?.getText().toString().trim() val mobile = myDialog?.布局中的一个text名字2?.getText().toString().trim() val info = myDialog?.布局中的一个text名字3?.getText().toString().trim() //测试一下 println(name + "——" + mobile + "——" + info) } } } } }

   

  5.测试结果

                

 

 

    6.OK!界面比较难看,但是基本原理就这样,自定义弹出输入框,弹出文字,弹出按钮,图片都类似于这样,

            当然方法还不只有这一种。以后再看看还有没有其他更好地方法。

posted @ 2017-07-03 20:50  Jason_Jan  阅读(930)  评论(0编辑  收藏  举报