Giter Site home page Giter Site logo

justinroom / keyboarddemo Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 8.0 23.79 MB

原生API实现可拖动、可缩放的自定软键盘。

Home Page: https://www.jianshu.com/p/8f3382a63f8b

License: Apache License 2.0

Java 100.00%
keyboard softinput inputmethod

keyboarddemo's Introduction

Keyboard

Download

自定义支持拖动、支持缩放的软键盘。适用于Activity、Fragment、DialogFragment。当手指在键盘上滑动超过8pixels时就进入了拖动模式。

一、扫描下载体验

二、Screenshots

  • 1、横屏键盘效果 lower_letter lower_letter_number upper_letter upper_letter_number number_abc number symbol

  • 2、竖屏键盘效果

lower_letter upper_letter number_abc number symbol

三、implementation

You may implementation recycler view support package first.

implementation 'com.android.support:appcompat-v7:xxx'
  • 1.1、Gradle
compile 'jsc.kit.keyboard:keyboard-component:_latestVersion'
  • 1.2、Maven
<dependency>
  <groupId>jsc.kit.keyboard</groupId>
  <artifactId>keyboard-component</artifactId>
  <version>_latestVersion</version>
  <type>pom</type>
</dependency>

四、Attribution

名称 类型 描述
keyboardType enum 键盘类型:numberletterletterNumber。仅支持在xml布局文件中显示效果,实际会根据EditTextinputType属性显示相应的键盘类型。
keyWidth dimension 按键的标准宽度,默认为 50dp。
keyHeight dimension 按键的标准高度,默认为keyWidth的五分之三。
keyHorizontalSpace dimension 按键水平间隙,默认为2dp。
keyVerticalSpace dimension 按键垂直间隙,默认为2dp。

五、Method

名称 返回类型 描述
initKeySize(int keyWidth, int keyHeight) void 初始化按键的标准宽高
initKeySpace(int horizontalSpace, int verticalSpace) void 初始化按键水平、垂直间隙
initCustomTypeface(Typeface typeface) void 设置按键上的字体
getKeyboardSize() int[] 获取键盘的宽高。当键盘为隐藏状态时,宽高都为0
toggleVisibility() void 代码调用强制显隐键盘
showKeyboard() void 代码调用强制显示键盘
closeKeyboard() void 代码调用强制关闭键盘
toggleNumberKeys() void 切换字母键盘上方的数字键显隐状态
setCreateKeyListener(onCreateKeyListener createKeyListener) void 设置创建每个按键时回调监听
setKeyboardListener(OnKeyboardListener keyboardListener) void 设置键盘的显隐监听
setKeyDownListener(OnKeyDownListener keyDownListener) void 自定义按键按下回调监听
addAllInputView(View view) void 管理view树中所有EditText
addInputView(@NonNull EditText editText) void 管理某个EditText
removeAllInputView(View view) void 移除view树中所有EditText
removeInputView(@NonNull EditText editText) void
getKeyboardType() String 获取当前显示的键盘类型
setDragSupportModel(@DragSupportModel String curDragSupportModel) void 设置键盘拖动方式
setDefaultUpperCase(boolean upperCase) void 设置默认显示大小写键盘
  • 1、键盘拖动方式
    public static final String ONLY_HORIZONTAL = "horizontal";//仅支持水平方向上拖动
    public static final String ONLY_VERTICAL = "vertical";//仅支持垂直方向上拖动
    public static final String ALL_DIRECTION = "all";//支持任意方向上拖动
    public static final String NONE = "none";//不支持拖动

    @StringDef({ONLY_HORIZONTAL, ONLY_VERTICAL, ALL_DIRECTION, NONE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface DragSupportModel {
    }
  • 2、监听创建按键回调
    public interface onCreateKeyListener {
        /**
         * @param keyboardType current keyboard type
         * @param key          the key of view was creating
         * @return label text sie. It's unit is dp.
         */
        float getKeyTextSize(@KeyUtils.KeyboardType String keyboardType, @KeyUtils.KeyCode int key);

        void onKeyCreated(boolean isCachedView, KeyView keyView, KeyBean bean);
    }
  • 3、监听键盘的显隐
    public interface OnKeyboardListener {
        void onShow(KeyboardView keyBoardView);

        void onHide(KeyboardView keyBoardView);
    }
  • 4、按下按键监听
    public interface OnKeyDownListener {
        boolean onKeyDown(KeyboardView keyBoardView, KeyView keyView);
    }

六、Usage

使用要点:

  • a、创建KeyboardView实例(支持xml布局文件):
KeyBoardView keyboardView = new KeyBoardView(context);
  • b、管理所有需要使用该自定义键盘的EditText
    //如果view是ViewGroup,自动查找该ViewGroup树下的所有EditText并加入管理
    public void addAllInputView(View view)

    //添加某一特定的EditText
    public void addInputView(@NonNull EditText editText)

    public void removeAllInputView(View view)

    public void removeInputView(@NonNull EditText editText)
  • c、把KeyboardView添加到ActivityFragmentDialogFragment所在的Window中: KeyUtils工具已提供了一个快速添加的方法。
KeyUtils.init(getActivity().getWindow(), keyboardView);
  • 1、Activity、Fragment。这里以Fragment为例:
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View root = inflater.inflate(R.layout.fragment_keyboard, container, false);
       keyboardView = new KeyBoardView(root.getContext());
       keyboardView.addAllInputView(root);
       KeyUtils.init(getActivity().getWindow(), keyboardView);

       root.findViewById(R.id.btn_toggle).setOnClickListener(this);
       root.findViewById(R.id.btn_dialog).setOnClickListener(this);
       return root;
   }
   
       @Override
       public void onResume() {
           super.onResume();
           keyboardView.onResume();
       }
   
       @Override
       public void onPause() {
           Log.i("KeyboardFragment", "onPause: ");
           keyboardView.onPause();
           super.onPause();
       }
   
       @Override
       public void onDestroy() {
           keyboardView.onDestroy();
           super.onDestroy();
       }

LICENSE

   Copyright 2019 JustinRoom

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

keyboarddemo's People

Contributors

justinroom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

keyboarddemo's Issues

作者你好,我仅修改了您的键盘布局,按照文档方法,添加到layout,但始终无法显示出来。

layout是RelativeLayout布局,其中具有RecyclerView,其item是包含又edit,我在item子view也就是edit的点击监听中添加了键盘显示,但始终不能奏效。我在其他空布局中只添加一个edit和键盘,点击edit时又能正常显示。很奇怪没有找到解决办法。

`

<LinearLayout
    android:id="@+id/data_title_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true">

    <com.xuexiang.xui.widget.actionbar.TitleBar
        android:id="@+id/data_title_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/xui_config_color_titlebar"
        app:tb_immersive="true"
        app:tb_titleText="@string/file"
        tools:layout_editor_absoluteX="0dp" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/data_title_view"
    android:layout_above="@+id/layout_view">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/qr_data_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusableInTouchMode="false"
            android:paddingTop="5dp" />

    </RelativeLayout>

</LinearLayout>

<LinearLayout
    android:id="@+id/float_btu_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginRight="15dp"
    android:layout_marginBottom="60dp"
    android:visibility="gone">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/keyboard_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/xui_btn_blue_normal_color"
        android:clickable="true"
        android:src="@drawable/ic_baseline_keyboard_hide_24"
        app:rippleColor="@color/xui_btn_blue_select_color" />

</LinearLayout>


<include
    android:id="@+id/keyBoard_view"
    layout="@layout/key_board_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />


<View
    android:id="@+id/btu_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_alignParentBottom="true"
    android:layout_centerVertical="true"
    android:layout_marginBottom="50dp" />

<RelativeLayout
    android:id="@+id/layout_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignTop="@+id/btu_view"
    android:background="@color/xui_btn_blue_normal_color">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/dp_10"
        android:gravity="center"
        android:orientation="horizontal">

        <com.xuexiang.xui.widget.button.roundbutton.RoundButton
            android:id="@+id/qr_create"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/qr_create"
            android:textColor="@color/xui_config_color_white" />


        <com.xuexiang.xui.widget.button.roundbutton.RoundButton
            android:id="@+id/qr_scanner"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/qr_scanner"
            android:textColor="@color/xui_config_color_white" />


        <com.xuexiang.xui.widget.button.roundbutton.RoundButton
            android:id="@+id/qr_browse"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/qr_browse"
            android:textColor="@color/xui_config_color_white" />

        <com.xuexiang.xui.widget.button.roundbutton.RoundButton
            android:id="@+id/qr_save"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_baseline_save_24"
            android:text="@string/save"
            android:textColor="@color/xui_config_color_white" />

        <ImageButton
            android:id="@+id/qr_kayBoard"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_baseline_keyboard_24" />

    </LinearLayout>

</RelativeLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/layout_view"
    android:gravity="center"
    android:layout_centerHorizontal="true">

    <jsc.kit.keyboard.KeyboardView
        android:id="@+id/float_keyBoard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:keyboardType="number"
        android:stateListAnimator="@null"/>

</LinearLayout>

`

`adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
@OverRide
public void onItemChildClick(@nonnull final BaseQuickAdapter adapter, @nonnull View view, final int position) {

            floatKeyBoard = (KeyboardView) findViewById(R.id.float_keyBoard); //浮动键盘

            final EditText data = (EditText) adapter.getViewByPosition(position, R.id.data_edit);

            floatKeyBoard.setDefaultUpperCase(false);
            //keyboardView.setBackgroundResource(R.drawable.key_keyboard_background_shape);
            floatKeyBoard.setPadding(0, 0, 0, 0);
            //keyboardView.addInputView(key_edit);
            KeyUtils.init(QRListActivity.this.getWindow(), floatKeyBoard);
            floatKeyBoard.setDragSupportModel(KeyboardView.ALL_DIRECTION);
            floatKeyBoard.addInputView(data);`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.