Activity实现弹出评论输入框及软键盘,按返回键同时消失

2017-12-18  本文已影响0人  吕志豪

首先效果展示


out.gif
  1. 创建Activity设置主题
    styles.xml中加入
 <style name="InputDialog" parent="Theme.AppCompat.Light.NoActionBar">
        //设置背景
        <item name="android:windowBackground">@android:color/transparent</item>
        //设置窗口和软键盘的交互模式
        <item name="android:windowSoftInputMode">adjustResize|stateVisible</item>
        //Dialog的windowFrame框为无
        <item name="android:windowFrame">@null</item>
        //是否显示标题,true则去掉默认的标题栏
        <item name="android:windowNoTitle">true</item>
        //是否浮现在activity之上,false的话会被软键盘覆盖
        <item name="android:windowIsFloating">true</item>
        //是否半透明,为false时背景为黑色不透明
        <item name="android:windowIsTranslucent">true</item>
        //是否有覆盖
        <item name="android:windowContentOverlay">@null</item>
        //Activity的动画效果
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <!--<item name="android:windowAnimationStyle">@style/DialogAnimation</item>-->
        //背景是否模糊显示,为false时效果为全透明
        <item name="android:backgroundDimEnabled">true</item>
        //点击空白处时是否销毁Activity
        <item name="android:windowCloseOnTouchOutside">true</item>
    </style>

AndroidManifest.xml中Activity引入该样式

        <activity android:name=".ui.InputDialogActivity" android:screenOrientation="portrait"
            android:theme="@style/InputDialog"/>
  1. 设置布局
<LinearLayout
        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:focusable="true"
            android:focusableInTouchMode="true"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <com.eqdd.yiqidian.widget.BackEditText
                android:id="@+id/et_comment"
                android:layout_width="match_parent"
                android:layout_height="200pt"
                android:layout_marginTop="15pt"
                android:background="@drawable/shape_12_oval_stroke_hint"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:gravity="start|top"
                android:hint="我來说一说~"
                android:padding="8pt"
                android:textSize="24pt" />

            <Button
                android:id="@+id/btn_submit"
                android:layout_width="130pt"
                android:layout_height="50pt"
                android:layout_gravity="end"
                android:layout_marginBottom="20pt"
                android:layout_marginTop="20pt"
                android:background="@drawable/selector06_comment"
                android:elevation="10pt"
                android:enabled="false"
                android:gravity="center"
                android:text="发表评论"
                android:textColor="@color/white"
                android:textSize="26pt"
                android:translationZ="10pt" />
        </LinearLayout>

    </LinearLayout>

注意事项:

public class BackEditText extends AppCompatEditText {
    public BackEditText(Context context) {
        super(context);
    }

    public BackEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BackEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public interface BackListener {
        void back(TextView textView);
    }


    private BackListener listener;

    public void setBackListener(BackListener listener) {
        this.listener = listener;
    }

    @Override

    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (listener != null) {
                listener.back(this);
                KeyboardUtil.hideSoftInput(this);
            }
        }
        return false;
    }
}
  1. Activity
public class InputDialogActivity extends BaseActivity {
        ......//设置布局文件后
        setWindow();
        showSoftInputFromWindow(this, etComment);
        etComment.setBackListener(textView -> {
            finish();
        });
        ......
    private void setWindow() {
        //窗口对齐屏幕宽度
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;//设置对话框置顶显示
        win.setAttributes(lp);
    }

    /**
     * EditText获取焦点并显示软键盘
     */
    public static void showSoftInputFromWindow(Activity activity, EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}

赏读至此,若心悦之,请点亮小红心♥️

上一篇下一篇

猜你喜欢

热点阅读