全国免费服务热线:

18982081108
✕

成都弹性云服务器,成都高防云服务器,互联网数据服务,成都祈钰瑶

Android使用Activity实现简单的可输入对话框-创新互联

1、需求分析

从策划到设计制作,每一步都追求做到细腻,制作可持续发展的企业网站。为客户提供成都网站制作、成都网站建设、网站策划、网页设计、域名注册、网络空间、网络营销、VI设计、 网站改版、漏洞修补等服务。为客户提供更好的一站式互联网解决方案,以客户的口碑塑造优易品牌,携手广大客户,共同发展进步。

众所周知,在应用中这样那样的评论总是少不了的,有的应用是在底部直接加一个EditText和一个Button,让用户输入文字或者表情之后点击按钮提交;而有的虽然也放置了EditText,但仅仅是一个“摆设”,并不具备输入功能,用户点击它后会弹出一个跳转到一个可以真正编辑的页面或者弹出一个可以输入内容的对话框。

比如下面这种效果:

这里的效果可以细分为四点:

  • 点击底部的按钮之后会弹出对话框,对话框在布局的底部;
  • 对话框中有输入框EditText,可以输入内容;
  • 对话框弹出后EditText会自动获取焦点,弹出软键盘;
  • 软键盘会把对话框顶上去,便于用户编辑。

一开始我想到的是PopupWindow,但是由于里面有EditText,与软键盘交互起来很是头疼,于是改用了Activity。这样一来我们就可以像用Activity一样使用这个对话框,方便多了。不过毕竟跟我们平时使用的Activity还是有所不同的,特别是要设置好它的样式,否则也是一堆的坑啊。

2、对话框Activity的布局与样式


下面就来着手实现我们想要的对话框了。新建一个工程,MainActivity只是一个配角,底部放一个按钮就搞定。我们的主角是DialogActivity,它的布局很简单,就跟平时的Activity一样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_gravity="bottom"
 android:background="@android:color/white"
 android:orientation="vertical"
 android:paddingLeft="10dp"
 android:paddingRight="10dp">

 <EditText
  android:id="@+id/et_comment"
  android:layout_width="match_parent"
  android:layout_height="150dp"
  android:layout_marginTop="15dp"
  android:background="#f0f0f0"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:gravity="left|top"
  android:hint="我來說一說~"
  android:paddingBottom="5dp"
  android:paddingLeft="8dp"
  android:paddingRight="8dp"
  android:paddingTop="5dp"
  android:textSize="14dp" />

 <Button
  android:textColor="@android:color/white"
  android:background="@android:color/holo_blue_light"
  android:id="@+id/btn_submit"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="right"
  android:layout_marginBottom="10dp"
  android:layout_marginTop="10dp"
  android:padding="5dp"
  android:text="發表評論"
  android:textSize="16sp" />
 </LinearLayout>

</LinearLayout>