前言
用于纯数字输入
布局示例
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<EditText
android:id="@+id/et_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/bg_edittext"
android:hint="键盘"
android:paddingBottom="12dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="12dp"
android:textSize="11dp"/>
<CheckBox
android:id="@+id/cb_flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/et_num"
android:layout_marginRight="20dp"
android:text="随机键盘"/>
<include
layout="@layout/item_keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
调用示例
public class MainActivity extends AppCompatActivity {
private EditText mEditText;
private CheckBox mCbFlag;
private KeyboardUtil mKeyboardUtil;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initEvent();
initListener();
}
/**
* 初始化监听事件
*/
private void initListener() {
mKeyboardUtil.setOnCuntomKeyboardListener(new KeyboardUtil.OnCuntomKeyboardListener() {
@Override
public void onCancel(boolean aBoolean) {
if (aBoolean) {
Toast.makeText(MainActivity.this, "键盘取消了", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onEnter(boolean aBoolean) {
if (aBoolean) {
Toast.makeText(MainActivity.this, "键盘按确定了", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* 初始化View事件
*/
private void initEvent() {
mEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mKeyboardUtil.attachTo(mEditText);
}
});
mCbFlag.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
mKeyboardUtil = new KeyboardUtil(MainActivity.this, true);
} else {
mKeyboardUtil = new KeyboardUtil(MainActivity.this);
}
mKeyboardUtil.hideKeyboard();
}
});
}
/**
* 初始化View数据
*/
private void initData() {
mKeyboardUtil = new KeyboardUtil(MainActivity.this, false);
}
/**
* 初始化View
*/
private void initView() {
mEditText = (EditText) findViewById(R.id.et_num);
mCbFlag = (CheckBox) findViewById(R.id.cb_flag);
}
}
运行截图
下载地址:Github