Activity實(shí)現(xiàn)彈出評(píng)論輸入框及軟鍵盤(pán),按返回鍵同時(shí)消失

首先效果展示


out.gif
  1. 創(chuàng)建Activity設(shè)置主題
    styles.xml中加入
 <style name="InputDialog" parent="Theme.AppCompat.Light.NoActionBar">
        //設(shè)置背景
        <item name="android:windowBackground">@android:color/transparent</item>
        //設(shè)置窗口和軟鍵盤(pán)的交互模式
        <item name="android:windowSoftInputMode">adjustResize|stateVisible</item>
        //Dialog的windowFrame框?yàn)闊o(wú)
        <item name="android:windowFrame">@null</item>
        //是否顯示標(biāo)題,true則去掉默認(rèn)的標(biāo)題欄
        <item name="android:windowNoTitle">true</item>
        //是否浮現(xiàn)在activity之上,false的話會(huì)被軟鍵盤(pán)覆蓋
        <item name="android:windowIsFloating">true</item>
        //是否半透明,為false時(shí)背景為黑色不透明
        <item name="android:windowIsTranslucent">true</item>
        //是否有覆蓋
        <item name="android:windowContentOverlay">@null</item>
        //Activity的動(dòng)畫(huà)效果
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <!--<item name="android:windowAnimationStyle">@style/DialogAnimation</item>-->
        //背景是否模糊顯示,為false時(shí)效果為全透明
        <item name="android:backgroundDimEnabled">true</item>
        //點(diǎn)擊空白處時(shí)是否銷(xiāo)毀Activity
        <item name="android:windowCloseOnTouchOutside">true</item>
    </style>

AndroidManifest.xml中Activity引入該樣式

        <activity android:name=".ui.InputDialogActivity" android:screenOrientation="portrait"
            android:theme="@style/InputDialog"/>
  1. 設(shè)置布局
<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="我來(lái)說(shuō)一說(shuō)~"
                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="發(fā)表評(píng)論"
                android:textColor="@color/white"
                android:textSize="26pt"
                android:translationZ="10pt" />
        </LinearLayout>

    </LinearLayout>

注意事項(xiàng):

  • EditView及其父容器設(shè)置焦點(diǎn)
    android:focusable="true"
    android:focusableInTouchMode="true"
  • 此處使用BackEditText,目的是為了攔截軟鍵盤(pán)彈出狀態(tài)下,返回鍵的監(jiān)聽(tīng)
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 {
        ......//設(shè)置布局文件后
        setWindow();
        showSoftInputFromWindow(this, etComment);
        etComment.setBackListener(textView -> {
            finish();
        });
        ......
    private void setWindow() {
        //窗口對(duì)齊屏幕寬度
        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;//設(shè)置對(duì)話框置頂顯示
        win.setAttributes(lp);
    }

    /**
     * EditText獲取焦點(diǎn)并顯示軟鍵盤(pán)
     */
    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);
    }
}

賞讀至此,若心悅之,請(qǐng)點(diǎn)亮小紅心??

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,281評(píng)論 25 708
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,377評(píng)論 0 17
  • 鏡像遷移
    ilkkzm閱讀 717評(píng)論 0 0
  • 你說(shuō)你從來(lái)都沒(méi)變過(guò),你知道我喜歡你,原來(lái)自己從來(lái)都是被玩弄于股掌之間的,原來(lái)你從來(lái)都沒(méi)有喜歡過(guò) 今天就這么違背...
    小丫頭慢慢來(lái)閱讀 328評(píng)論 0 0
  • 淡,所以不苦。甜到濃時(shí)會(huì)變苦,苦到濃處心無(wú)力。生活如水,欲望如糖,不是糖越多,水就越甜。適當(dāng)?shù)淖非螅苁股钐鹈郏?..
    董夢(mèng)雅閱讀 384評(píng)論 0 1

友情鏈接更多精彩內(nèi)容