因项目需求,先随意写了个密码键盘的demo,类似支付时输入的6位数字密码,也算是一边学一边写了,不说废话上代码了
先看自定义的view吧
这里用了xml布局文件,看过一个比较成熟的密码键盘,全都是用代码画上去的,然后用onTouchEvent判断坐标实现各种点击功能,这里为了省力就用布局了
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.example.demo_custom_pwdkeyboard.MainActivity$PlaceholderFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#858E9A"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<EditText
android:id="@id/et_keyboard_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edt_border"
android:ems="6"
android:gravity="center_vertical|right"
android:paddingRight="10dp"
android:singleLine="false" >
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@id/btn_keyboard_1"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/btn_style_alert_dialog_button_normal"
android:text="@string/btn_keyboard_1" />
<Button
android:id="@id/btn_keyboard_2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/btn_style_alert_dialog_button_normal"
android:text="@string/btn_keyboard_2" />
<Button
android:id="@id/btn_keyboard_3"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/btn_style_alert_dialog_button_normal"
android:text="@string/btn_keyboard_3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@id/btn_keyboard_4"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/btn_style_alert_dialog_button_normal"
android:text="@string/btn_keyboard_4" />
<Button
android:id="@id/btn_keyboard_5"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/btn_style_alert_dialog_button_normal"
android:text="@string/btn_keyboard_5&#