Android 類似新聞界面評(píng)論框

前言:項(xiàng)目中用到了界面詳情評(píng)論功能這里小記一下

界面展示


圖一


圖二

如圖? 圖一紅框是一個(gè)LinearLayout布局包裹的TextView,當(dāng)點(diǎn)擊請(qǐng)輸入的時(shí)候 彈出自定義dialog如圖二

代碼步驟 (這里不再展示activity界面布局)

1.創(chuàng)建dialog布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="match_parent"

? ? android:layout_height="wrap_content"

? ? android:background="@color/beijinghui"

? ? android:orientation="horizontal">

? ? ? ? android:id="@+id/ed_comment"

? ? ? ? android:layout_width="0dp"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_gravity="center_vertical"

? ? ? ? android:layout_margin="@dimen/dp_10"

? ? ? ? android:layout_weight="1"

? ? ? ? android:background="@drawable/hui_kong_bg"

? ? ? ? android:hint="請(qǐng)輸入您要評(píng)論的內(nèi)容"

? ? ? ? android:maxLines="4"

? ? ? ? android:padding="@dimen/dp_7"

? ? ? ? android:textColor="#434350"

? ? ? ? android:textColorHint="#AFAFAF"

? ? ? ? android:textSize="@dimen/sp_12" />

? ? ? ? android:id="@+id/btn_fabu_pl"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_gravity="center_vertical"

? ? ? ? android:padding="@dimen/dp_10"

? ? ? ? android:text="發(fā)布"

? ? ? ? android:textColor="#434350"

? ? ? ? android:textSize="@dimen/sp_14" />

</LinearLayout>

2.點(diǎn)擊按鈕彈出dialog

/**

* 初始化dialog

*/

private void showDialog() {

Dialog dialog =new Dialog(this);

? ? dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

? ? dialog.setContentView(R.layout.popu_comment);

? ? Window dialogWindow = dialog.getWindow();

? ? dialogWindow.setGravity(Gravity.BOTTOM);

? ? dialogWindow.getDecorView().setPadding(0, 0, 0, 0);

? ? WindowManager.LayoutParams lp = dialogWindow.getAttributes();

? ? lp.width = WindowManager.LayoutParams.MATCH_PARENT;

? ? lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

? ? dialogWindow.setAttributes(lp);

? ? dialog.show();

? ? //查找控件

? ? EditText edComment = dialog.findViewById(R.id.ed_comment);

? ? TextView btnFaBu = dialog.findViewById(R.id.btn_fabu_pl);

? ? btnFaBu.setOnClickListener(new View.OnClickListener() {

@Override

? ? ? ? public void onClick(View v) {

dialog.dismiss();

? ? ? ? ? ? //提交之后要講str清空

? ? ? ? ? ? str ="";

? ? ? ? ? ? //展示框復(fù)原

? ? ? ? ? ? tvContent.setText("請(qǐng)輸入您的評(píng)論內(nèi)容");

? ? ? ? }

});

? ? //判斷有沒(méi)有編輯過(guò)評(píng)論內(nèi)容 如果編輯過(guò)就在顯示出來(lái)

? ? if (!TextUtils.isEmpty(str)) {

????????edComment.setText(str);

? ? ? ? edComment.setSelection(str.length());//將光標(biāo)移至文字末尾

? ? }

//添加監(jiān)聽(tīng)

? ? edComment.addTextChangedListener(new TextWatcher() {

????????@Override

? ? ? ? public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

????????}

????????@Override

? ? ? ? public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

????????}

????????@Override

? ? ? ? public void afterTextChanged(Editable editable) {

????????????str =edComment.getText().toString();

? ? ? ? ? ? tvContent.setText(str);

? ? ? ? }

});

? ? showSoft();

? ? dismissSofo(dialog);

}

3.當(dāng)點(diǎn)擊白色位置時(shí),關(guān)閉dialog這個(gè)時(shí)候也要將鍵盤關(guān)閉

/**

* 關(guān)閉軟鍵盤

*

* @param dialog

*/

private void dismissSofo(Dialog dialog) {

//針對(duì)dialog隱藏做一個(gè)監(jiān)聽(tīng)? 當(dāng)dialog隱藏的時(shí)候 就關(guān)閉

? ? dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

????????@Override

? ? ? ? public void onDismiss(DialogInterface dialog) {

????????????InputMethodManager inputMgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

? ? ? ? ? ? inputMgr.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);

? ? ? ? }

});

}

4.顯示dilog的同時(shí)開(kāi)啟鍵盤

/**

* 開(kāi)啟軟鍵盤

*/

? ? private void showSoft() {

????????Handler handle =new Handler();

? ? ? ? handle.postDelayed(new Runnable() {

????????????@Override

? ? ? ? ? ? public void run() {

? ? ? ? ? ? ? ? InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

? ? ? ? ? ? ? ? imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

? ? ? ? ? ????? }

????????????}, 100);

? ????? }

?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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