前言
用于純數(shù)字輸入
布局示例
<?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="隨機(jī)鍵盤"/>
<include
layout="@layout/item_keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
調(diào)用示例
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();
}
/**
* 初始化監(jiān)聽事件
*/
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數(shù)據(jù)
*/
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);
}
}
運(yùn)行截圖
下載地址:Github