服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现可输入数据的弹出框

Android实现可输入数据的弹出框

2021-05-08 16:53无缘公子 Android

这篇文章主要为大家详细介绍了Android实现可输入数据的弹出框,文章提供了两种方式,示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

之前一篇文章,介绍了如何定义从屏幕底部弹出popupwindow即《android animation实战之屏幕底部弹出popupwindow》写完之后,突然想起之前写过自定义内容显示的弹出框,就随手写了两个实例,分享出来:

Android实现可输入数据的弹出框

第一种实现方式:继承dialog
 1.1 线定义弹出框要显示的内容:create_user_dialog.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/create_user_dialog_view"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:background="@drawable/dialog_load_bg"
 android:minwidth="200dp"
 android:orientation="vertical"
 android:padding="10dp"
 android:paddingbottom="30dp"
 android:paddingtop="30dp">
 
 <edittext
  android:id="@+id/text_name"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/edit_bg"
  android:hint="姓名"
  android:minheight="45dp"
  android:textsize="18sp" />
 
 <edittext
  android:id="@+id/text_mobile"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_margintop="5dp"
  android:background="@drawable/edit_bg"
  android:hint="手机号"
  android:minheight="45dp"
  android:textsize="18sp" />
 
 <edittext
  android:id="@+id/text_info"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_margintop="5dp"
  android:background="@drawable/edit_bg"
  android:gravity="top|left"
  android:hint="个性签名"
  android:minheight="145dp"
  android:textsize="18sp" />
 
 <button
  android:id="@+id/btn_save_pop"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_margintop="5dp"
  android:text="保存" />
 
</linearlayout>

 1.2 定义要弹出的dialog

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class createuserdialog extends dialog {
 
 /**
  * 上下文对象 *
  */
 activity context;
 
 private button btn_save;
 
 public edittext text_name;
 
 public edittext text_mobile;
 
 public edittext text_info;
 
 
 private view.onclicklistener mclicklistener;
 
 public createuserdialog(activity context) {
  super(context);
  this.context = context;
 }
 
 public createuserdialog(activity context, int theme, view.onclicklistener clicklistener) {
  super(context, theme);
  this.context = context;
  this.mclicklistener = clicklistener;
 }
 
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  // 指定布局
  this.setcontentview(r.layout.create_user_dialog);
 
  text_name = (edittext) findviewbyid(r.id.text_name);
  text_mobile = (edittext) findviewbyid(r.id.text_mobile);
  text_info = (edittext) findviewbyid(r.id.text_info);
 
  /*
   * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getwindow(),表示获得这个activity的window
   * 对象,这样这可以以同样的方式改变这个activity的属性.
   */
  window dialogwindow = this.getwindow();
 
  windowmanager m = context.getwindowmanager();
  display d = m.getdefaultdisplay(); // 获取屏幕宽、高用
  windowmanager.layoutparams p = dialogwindow.getattributes(); // 获取对话框当前的参数值
  // p.height = (int) (d.getheight() * 0.6); // 高度设置为屏幕的0.6
  p.width = (int) (d.getwidth() * 0.8); // 宽度设置为屏幕的0.8
  dialogwindow.setattributes(p);
 
  // 根据id在布局中找到控件对象
  btn_save = (button) findviewbyid(r.id.btn_save);
 
  // 为按钮绑定点击事件监听器
  btn_save.setonclicklistener(mclicklistener);
 
  this.setcancelable(true);
 }
}

1.3 调用弹出框

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void showeditdialog(view view) {
 createuserdialog = new createuserdialog(this,r.style.loading_dialog,onclicklistener);
 createuserdialog.show();
}
 
 
 
private view.onclicklistener onclicklistener = new view.onclicklistener() {
  @override
  public void onclick(view v) {
 
   switch (v.getid()) {
    case r.id.btn_save:
 
     string name = createuserdialog.text_name.gettext().tostring().trim();
     string mobile = createuserdialog.text_mobile.gettext().tostring().trim();
     string info = createuserdialog.text_info.gettext().tostring().trim();
 
     system.out.println(name+"——"+mobile+"——"+info);
     break;
   }
  }
 };

第二种实现方式:继承popupwindow
2.1 定义弹出框布局文件,和1.1定义的一致
2.2 定义要弹出的popupwindow

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class createuserpopwin extends popupwindow {
 
 private context mcontext;
 
 private view view;
 
 private button btn_save_pop;
 
 public edittext text_name;
 
 public edittext text_mobile;
 
 public edittext text_info;
 
 
 public createuserpopwin(activity mcontext, view.onclicklistener itemsonclick) {
 
  this.mcontext = mcontext;
 
  this.view = layoutinflater.from(mcontext).inflate(r.layout.create_user_pop, null);
 
  text_name = (edittext) view.findviewbyid(r.id.text_name);
  text_mobile = (edittext) view.findviewbyid(r.id.text_mobile);
  text_info = (edittext) view.findviewbyid(r.id.text_info);
 
  btn_save_pop = (button) view.findviewbyid(r.id.btn_save_pop);
 
  // 设置按钮监听
  btn_save_pop.setonclicklistener(itemsonclick);
 
  // 设置外部可点击
  this.setoutsidetouchable(true);
 
 
  /* 设置弹出窗口特征 */
  // 设置视图
  this.setcontentview(this.view);
 
  // 设置弹出窗体的宽和高
   /*
   * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getwindow(),表示获得这个activity的window
   * 对象,这样这可以以同样的方式改变这个activity的属性.
   */
  window dialogwindow = mcontext.getwindow();
 
  windowmanager m = mcontext.getwindowmanager();
  display d = m.getdefaultdisplay(); // 获取屏幕宽、高用
  windowmanager.layoutparams p = dialogwindow.getattributes(); // 获取对话框当前的参数值
 
  this.setheight(relativelayout.layoutparams.wrap_content);
  this.setwidth((int) (d.getwidth() * 0.8));
 
  // 设置弹出窗体可点击
  this.setfocusable(true);
 
 }
}

2.3 调用该弹框组件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public void showeditpopwin(view view) {
 
  createuserpopwin = new createuserpopwin(this,onclicklistener);
 
  createuserpopwin.showatlocation(findviewbyid(r.id.main_view), gravity.center, 0, 0);
 }
 
 
private view.onclicklistener onclicklistener = new view.onclicklistener() {
  @override
  public void onclick(view v) {
 
   switch (v.getid()) {
    case r.id.btn_save_pop:
 
     string name1 = createuserpopwin.text_name.gettext().tostring().trim();
     string mobile1 = createuserpopwin.text_mobile.gettext().tostring().trim();
     string info1 = createuserpopwin.text_info.gettext().tostring().trim();
 
     system.out.println(name1+"——"+mobile1+"——"+info1);
 
     createuserpopwin.dismiss();
     break;
   }
  }
 };

以上就是本文的全部内容,希望对大家的学习android有所帮助。

延伸 · 阅读

精彩推荐
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

    Android CardView+ViewPager实现ViewPager翻页动画的方法

    本篇文章主要介绍了Android CardView+ViewPager实现ViewPager翻页动画的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Abby代黎明9602022-03-02
  • AndroidAndroid实现固定屏幕显示的方法

    Android实现固定屏幕显示的方法

    这篇文章主要介绍了Android实现固定屏幕显示的方法,实例分析了Android屏幕固定显示所涉及的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    鉴客6192021-03-27
  • Android汇总Android视频录制中常见问题

    汇总Android视频录制中常见问题

    这篇文章主要汇总了Android视频录制中常见问题,帮助大家更好地解决Android视频录制中常见的问题,需要的朋友可以参考下...

    yh_thu5192021-04-28
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

    Android界面效果UI开发资料汇总(附资料包)

    android ui界面设计,友好的界面会提高用户体验度;同时也增强了android ui界面设计的难度,本文提供了一些常用开发资料(有下载哦)感兴趣的朋友可以了解下...

    Android开发网4672021-01-03
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

    这篇文章主要介绍了Android中AsyncTask详细介绍,AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合,需要的朋友可以参考下...

    Android开发网7452021-03-11
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

    这篇文章主要介绍了Android程序设计的AIDL,以一个完整实例的形式较为详细的讲述了AIDL的原理及实现方法,需要的朋友可以参考下...

    Android开发网4642021-03-09
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

    Android编程解析XML方法详解(SAX,DOM与PULL)

    这篇文章主要介绍了Android编程解析XML方法,结合实例形式详细分析了Android解析XML文件的常用方法与相关实现技巧,需要的朋友可以参考下...

    liuhe68810052021-05-03
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

    Android实现Service获取当前位置(GPS+基站)的方法

    这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Ruthless8342021-03-31