码迷,mamicode.com
首页 > 其他好文 > 详细

自定义数字软键盘--封装版

时间:2017-08-30 10:58:24      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:--   XML   sse   schema   manage   rdsh   cal   mdi   print   

布局:

<?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="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <TextView
            android:id="@+id/number_1"
            style="@style/number_keyboard"
            android:text="1" />

        <TextView
            android:id="@+id/number_2"
            style="@style/number_keyboard"
            android:text="2" />

        <TextView
            android:id="@+id/number_3"
            style="@style/number_keyboard"
            android:text="3" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <TextView
            android:id="@+id/number_4"
            style="@style/number_keyboard"
            android:text="4" />

        <TextView
            android:id="@+id/number_5"
            style="@style/number_keyboard"
            android:text="5" />

        <TextView
            android:id="@+id/number_6"
            style="@style/number_keyboard"
            android:text="6" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <TextView
            android:id="@+id/number_7"
            style="@style/number_keyboard"
            android:text="7" />

        <TextView
            android:id="@+id/number_8"
            style="@style/number_keyboard"
            android:text="8" />

        <TextView
            android:id="@+id/number_9"
            style="@style/number_keyboard"
            android:text="9" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp">

        <ImageView
            android:id="@+id/number_clear_last"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:padding="15dp"
            android:src="@drawable/keyboard_delete_img"
            android:background="@drawable/number_keyboard_selecter"/>

        <TextView
            android:id="@+id/number_0"
            style="@style/number_keyboard"
            android:text="0" />

        <TextView
            android:id="@+id/number_enter"
            style="@style/number_keyboard_enter"
            android:text="进入"
            android:textColor="@drawable/keyboard_enter_textcolor_selecter"/>
    </LinearLayout>
</LinearLayout>

 

风格:

    <style name="number_keyboard">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">1</item>
        <item name="android:textSize">24sp</item>
        <item name="android:gravity">center</item>
        <item name="android:background">@drawable/number_keyboard_selecter</item>
    </style>

    <style name="number_keyboard_enter">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">1</item>
        <item name="android:textSize">24sp</item>
        <item name="android:gravity">center</item>
        <item name="android:background">@drawable/number_keyboard_enter_selecter</item>
    </style>

 

selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <stroke android:width="0.5dp" android:color="@color/ccccccc"></stroke>
            <solid android:color="@color/c4188fe"></solid>
        </shape>
    </item>

    <item android:state_pressed="false">
        <shape>
            <stroke android:width="0.5dp" android:color="@color/ccccccc"></stroke>
            <solid android:color="@color/white"></solid>
        </shape>
    </item>
</selector>

 

java类:

public class NumberKeyboardDialog extends RelativeLayout {

    @BindView(R.id.number_1)
    TextView number1;
    @BindView(R.id.number_2)
    TextView number2;
    @BindView(R.id.number_3)
    TextView number3;
    @BindView(R.id.number_4)
    TextView number4;
    @BindView(R.id.number_5)
    TextView number5;
    @BindView(R.id.number_6)
    TextView number6;
    @BindView(R.id.number_7)
    TextView number7;
    @BindView(R.id.number_8)
    TextView number8;
    @BindView(R.id.number_9)
    TextView number9;
    @BindView(R.id.number_clear_last)
    ImageView numberClearLast;
    @BindView(R.id.number_0)
    TextView number0;
    @BindView(R.id.number_enter)
    TextView numberEnter;

    private NumberClickListener numberClickListener;
    private AlertDialog numberDialog;

    public NumberKeyboardDialog(Context context) {
        super(context);
    }

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

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

    public void show(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.number_keyboard, null);
        ButterKnife.bind(this, view);
        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.DialogTheme);
        builder.setView(view);
        numberDialog = builder.create();
        BottomDialogUtils.setBottom(numberDialog);
        numberDialog.show();
    }

    @OnClick({R.id.number_1, R.id.number_2, R.id.number_3, R.id.number_4, R.id.number_5, R.id.number_6, R.id.number_7, R.id.number_8, R.id.number_9, R.id.number_0, R.id.number_clear_last, R.id.number_enter})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.number_1:
                numberClickListener.clickNumber(number1.getText().toString());
                break;
            case R.id.number_2:
                numberClickListener.clickNumber(number2.getText().toString());
                break;
            case R.id.number_3:
                numberClickListener.clickNumber(number3.getText().toString());
                break;
            case R.id.number_4:
                numberClickListener.clickNumber(number4.getText().toString());
                break;
            case R.id.number_5:
                numberClickListener.clickNumber(number5.getText().toString());
                break;
            case R.id.number_6:
                numberClickListener.clickNumber(number6.getText().toString());
                break;
            case R.id.number_7:
                numberClickListener.clickNumber(number7.getText().toString());
                break;
            case R.id.number_8:
                numberClickListener.clickNumber(number8.getText().toString());
                break;
            case R.id.number_9:
                numberClickListener.clickNumber(number9.getText().toString());
                break;
            case R.id.number_0:
                numberClickListener.clickNumber0(number0.getText().toString());
                break;
            case R.id.number_clear_last:
                numberClickListener.clickClearLast();
                break;
            case R.id.number_enter:
                numberClickListener.clickEnter();
                numberDialog.dismiss();
                break;
        }
    }

    @OnLongClick({R.id.number_clear_last})
    public boolean onLongClick(View view) {
        switch (view.getId()) {
            case R.id.number_clear_last:
                numberClickListener.longClickClearLast();
                break;
        }
        return false;
    }


    public void setNumberClickListener(NumberClickListener numberClickListener) {
        this.numberClickListener = numberClickListener;
    }

    public interface NumberClickListener {
        void clickNumber(String number);

        void clickNumber0(String number0);

        void clickClearLast();

        void longClickClearLast();

        void clickEnter();
    }
}

 

调用:

 /**
     * 房间号输入监听
     */
    private void setRoomNumberClear() {
        roomIdInput.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (roomIdInput.getText().toString().length() > 0) {//限制长度
                    roomIdClear.setVisibility(View.VISIBLE);
                } else {
                    roomIdClear.setVisibility(View.GONE);
                }
            }
        });

        SoftKeyBoardListener.setListener(getActivity(), new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
                if (null == getActivity()) {
                    return;
                }
                ((MainActivity) getActivity()).main_menu.setVisibility(View.GONE);
                ((MainActivity) getActivity()).layoutCommunity.setVisibility(View.GONE);
            }

            @Override
            public void keyBoardHide(int height) {
                if (null == getActivity()) {
                    return;
                }
                ((MainActivity) getActivity()).main_menu.setVisibility(View.VISIBLE);
                ((MainActivity) getActivity()).layoutCommunity.setVisibility(View.VISIBLE);
            }
        });
    }

    private void initNumberListener() {
        numberDialog.setNumberClickListener(new NumberKeyboardDialog.NumberClickListener() {
            @Override
            public void clickNumber(String number) {
                String roomInput = roomIdInput.getText().toString();
                roomIdInput.setText(roomInput + number);
                roomIdInput.setSelection(roomInput.length() + 1);
            }

            @Override
            public void clickNumber0(String number0) {
                String roomInput = roomIdInput.getText().toString();
                //0不能在第一位
                if (!TextUtils.isEmpty(roomInput)) {
                    roomIdInput.setText(roomInput + number0);
                    roomIdInput.setSelection(roomInput.length() + 1);
                }
            }

            @Override
            public void clickClearLast() {
                String roomInput = roomIdInput.getText().toString();
                if (roomIdInput.length() > 0) {
                    roomIdInput.setText(roomInput.substring(0, roomIdInput.length() - 1));
                    roomIdInput.setSelection(roomInput.length() - 1);
                }
            }

            @Override
            public void longClickClearLast() {
                roomIdInput.setText("");
            }

            @Override
            public void clickEnter() {
                final String roomId = roomIdInput.getText().toString();
                if (TextUtils.isEmpty(roomId)) {
                    Toast.makeText(getActivity(), "请输入房间号", Toast.LENGTH_SHORT).show();
                } else {
                    LiveRoomAPI.getTopicInfo(roomId, new BaseCallBack<TopicInfoResponseEntity>() {
                        @Override
                        public void onSuccess(Response<TopicInfoResponseEntity> response) {
                            //跳转到直播间
                            Intent intent = new Intent(getActivity(), LiveroomInfoActivity.class);
                            intent.putExtra(IntentKey.topicId, roomId);
                            startActivity(intent);
                        }

                        @Override
                        public void onFailure(StateException exception) {
                            Toast.makeText(getActivity(), exception.getMessage().toString(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }

    /**
     * 设置不调用系统键盘
     */
    private void initKeyBoard() {
        if (android.os.Build.VERSION.SDK_INT <= 10) {
            roomIdInput.setInputType(InputType.TYPE_NULL);
        } else {
            getActivity().getWindow().setSoftInputMode(
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            try {
                Class<EditText> cls = EditText.class;
                Method setShowSoftInputOnFocus;
                setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus",
                        boolean.class);
                setShowSoftInputOnFocus.setAccessible(true);
                setShowSoftInputOnFocus.invoke(roomIdInput, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        initNumberListener();
    }

 

封装起来更好用

自定义数字软键盘--封装版

标签:--   XML   sse   schema   manage   rdsh   cal   mdi   print   

原文地址:http://www.cnblogs.com/anni-qianqian/p/7451853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!