Android dialog類型輸入框 來源於騰訊小直播項目

package com.jjdance.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jjdance.R;


/**
 * 文本輸入框
 */
public class TCInputTextMsgDialog extends Dialog {

    public interface OnTextSendListener {

       void onTextSend(String msg, boolean tanmuOpen);
        void onSoftKeyShow(int height);
    }
    private TextView confirmBtn;
    private LinearLayout mBarrageArea;
    private EditText messageTextView;
    private static final String TAG = TCInputTextMsgDialog.class.getSimpleName();
    private Context mContext;
    private InputMethodManager imm;
    private RelativeLayout rlDlg;
    private int mLastDiff = 0;
    private LinearLayout mConfirmArea;
    private OnTextSendListener mOnTextSendListener;
    private boolean mDanmuOpen = false;
//    private final String reg = "[`~@#$%^&*()-_+=|{}':;,/.<>¥…()—【】‘;:”“’。,、]";
//    private Pattern pattern = Pattern.compile(reg);

    public TCInputTextMsgDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        setContentView(R.layout.dialog_input_text);

        messageTextView = (EditText) findViewById(R.id.et_input_message);
        messageTextView.setInputType(InputType.TYPE_CLASS_TEXT);
        //修改下劃線顏色
        messageTextView.getBackground().setColorFilter(context.getResources().getColor(R.color.transparent), PorterDuff.Mode.CLEAR);

        messageTextView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s.toString().trim())){
                    confirmBtn.setBackgroundResource(R.drawable.shape_gray);
                }else{
                    confirmBtn.setBackgroundResource(R.drawable.shape_green);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        confirmBtn = (TextView) findViewById(R.id.confrim_btn);
        imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msg = messageTextView.getText().toString().trim();
                if (!TextUtils.isEmpty(msg)) {

                    mOnTextSendListener.onTextSend(msg, mDanmuOpen);
                    imm.showSoftInput(messageTextView, InputMethodManager.SHOW_FORCED);
                    imm.hideSoftInputFromWindow(messageTextView.getWindowToken(), 0);
                    messageTextView.setText("");
                    dismiss();
                } else {
//                    Toast.makeText(mContext, "input can not be empty!", Toast.LENGTH_LONG).show();
                }
                messageTextView.setText(null);
            }
        });

//        final Button barrageBtn = (Button) findViewById(R.id.barrage_btn);
//        barrageBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mDanmuOpen = !mDanmuOpen;
//                if (mDanmuOpen) {
//                    barrageBtn.setBackgroundResource(R.mipmap.barrage_slider_on);
//                } else {
//                    barrageBtn.setBackgroundResource(R.mipmap.barrage_slider_off);
//                }
//            }
//        });

        mBarrageArea = (LinearLayout) findViewById(R.id.barrage_area);
        mBarrageArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDanmuOpen = !mDanmuOpen;
//                if (mDanmuOpen) {
//                    barrageBtn.setBackgroundResource(R.mipmap.barrage_slider_on);
//                } else {
//                    barrageBtn.setBackgroundResource(R.mipmap.barrage_slider_off);
//                }
            }
        });

        messageTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                switch (actionId) {
                    case KeyEvent.KEYCODE_ENDCALL:
                    case KeyEvent.KEYCODE_ENTER:
                        if (messageTextView.getText().length() > 0) {
                            imm.hideSoftInputFromWindow(messageTextView.getWindowToken(), 0);
                            dismiss();
                        } else {
//                            Toast.makeText(mContext, "input can not be empty!", Toast.LENGTH_LONG).show();
                        }
                        return true;
                    case KeyEvent.KEYCODE_BACK:
                        dismiss();
                        return false;
                    default:
                        return false;
                }
            }
        });

        mConfirmArea = (LinearLayout) findViewById(R.id.confirm_area);
        mConfirmArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = messageTextView.getText().toString().trim();
                if (!TextUtils.isEmpty(msg)) {

                    mOnTextSendListener.onTextSend(msg, mDanmuOpen);
                    imm.showSoftInput(messageTextView, InputMethodManager.SHOW_FORCED);
                    imm.hideSoftInputFromWindow(messageTextView.getWindowToken(), 0);
                    messageTextView.setText("");
                    dismiss();
                } else {
//                    Toast.makeText(mContext, R.string.input_no_empty, Toast.LENGTH_LONG).show();
                }
                messageTextView.setText(null);
            }
        });

        messageTextView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                Log.d("livelog", "onKey " + keyEvent.getCharacters());
                return false;
            }
        });

        rlDlg = (RelativeLayout) findViewById(R.id.rl_outside_view);
        rlDlg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v.getId() != R.id.rl_inputdlg_view)
                    dismiss();
            }
        });

        final LinearLayout rldlgview = (LinearLayout) findViewById(R.id.rl_inputdlg_view);

        rldlgview.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
                Rect r = new Rect();
                //獲取當前界面可視部分
                getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //獲取屏幕的高度
                int screenHeight =  getWindow().getDecorView().getRootView().getHeight();
                int m = getWindow().getDecorView().getHeight();
                //此處就是用來獲取鍵盤的高度的, 在鍵盤沒有彈出的時候 此高度爲0 鍵盤彈出的時候爲一個正數
                int heightDifference = screenHeight - r.bottom ;

                if (heightDifference <= 0 && mLastDiff > 0){
                    //imm.hideSoftInputFromWindow(messageTextView.getWindowToken(), 0);
                    dismiss();
                }
                if (heightDifference >0){
                    if (mOnTextSendListener != null){
                        mOnTextSendListener.onSoftKeyShow(heightDifference);
                    }
                }
                mLastDiff = heightDifference;
            }
        });
        rldlgview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imm.hideSoftInputFromWindow(messageTextView.getWindowToken(), 0);
                dismiss();
            }
        });
    }

    public void setmOnTextSendListener(OnTextSendListener onTextSendListener) {
        this.mOnTextSendListener = onTextSendListener;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        //dismiss之前重置mLastDiff值避免下次無法打開
        mLastDiff = 0;
    }

    @Override
    public void show() {
        super.show();
    }
}

使用方法

//發消息文本框
        mInputTextMsgDialog = new TCInputTextMsgDialog(this, R.style.InputDialog);
        mInputTextMsgDialog.setmOnTextSendListener(this);
<!-- Input dialog theme. -->
    <style name="InputDialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">false</item>
        <item name="android:windowFullscreen">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">#00000000</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">false</item>
       <!-- <item name="colorControlNormal">@color/text_5a5b</item>
        <item name="android:textColorHint">@color/gray</item>
        <item name="colorControlActivated">@color/text_5a5b</item>-->
    </style>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_outside_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
<!--android:background="@color/color_input_dialog_background"-->
    <LinearLayout
        android:id="@+id/rl_inputdlg_view"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">

        <LinearLayout
            android:id="@+id/barrage_area"
            android:orientation="horizontal"
            android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            >


        </LinearLayout>

        <View
            android:layout_width="0.5dp"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="6dp"
            android:layout_marginStart="6dp"
            android:visibility="gone"
            android:background="@color/line"/>

        <EditText
            android:id="@+id/et_input_message"
            android:hint="說點什麼吧。。"
            android:layout_width="0dp"
            android:layout_weight="4"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:layout_gravity="center_vertical"
            android:imeActionId="@+id/login"
            android:imeOptions="actionUnspecified"
            android:inputType="text"
            android:maxLength="32"
            android:textColor="@color/color_222222"
            android:maxLines="1"
            android:singleLine="true" />

        <!--<View android:layout_width="0.5dp"-->
        <!--android:layout_height="match_parent"-->
        <!--android:layout_marginTop="10dp"-->
        <!--android:layout_marginBottom="10dp"-->
        <!--android:layout_marginLeft="6dp"-->
        <!--android:layout_marginStart="6dp"-->
        <!--android:background="@color/line"/>-->

        <LinearLayout
            android:id="@+id/confirm_area"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingRight="10dp"
            android:gravity="center"
            >

            <Button
                android:id="@+id/confrim_btn"
                android:layout_width="50dp"
                android:layout_height="25dp"
                android:background="@drawable/shape_gray"
                android:textColor="@color/white"
                android:text="@string/send" />
        </LinearLayout>


    </LinearLayout>

</RelativeLayout>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章