79.android 简单的拨号键盘表格布局与EditText配合使用,只要EditText的光标不要软键盘+删除光标前与添加光标后的字符

 //第一步 我的Activity表格布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/mDial_dial"
    >

    <View
        android:background="@color/CAE"
        android:layout_width="match_parent"
        android:layout_height="1dp"></View>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:drawablePadding="10dp"
            android:padding="10dp"
            android:drawableTop="@mipmap/btn_create_customer"
            android:gravity="center"
            android:textSize="12sp"
            android:text="添加客户"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
        <TextView
            android:drawablePadding="10dp"
            android:padding="10dp"
            android:drawableTop="@mipmap/btn_message"
            android:gravity="center"
            android:textSize="12sp"
            android:text="发送短信"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="6.5dip" >

        <TableRow
            android:background="@color/CAE"
            android:orientation="horizontal"
            android:paddingLeft="0.5dip"
            android:paddingRight="0.5dip"
            android:paddingTop="1dip" >

            <ImageButton
                android:id="@+id/imageView1"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="0.5dip"
                android:layout_marginLeft="0.5dip"
                android:background="@drawable/layout_color"
                android:src="@mipmap/btn_1" />

            <ImageButton
                android:id="@+id/imageView2"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="0.5dip"
                android:layout_marginLeft="0.5dip"
                android:background="@drawable/layout_color"
                android:src="@mipmap/btn_2" />

            <ImageButton
                android:id="@+id/imageView3"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="0.5dip"
                android:layout_marginLeft="0.5dip"
                android:background="@drawable/layout_color"
                android:src="@mipmap/btn_3" />
        </TableRow>

        <TableRow
            android:background="@color/CAE"
            android:orientation="horizontal"
            android:paddingLeft="0.5dip"
            android:paddingRight="0.5dip"
            android:paddingTop="0dip" >

            <ImageButton
                android:id="@+id/imageView4"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="0.5dip"
                android:layout_marginLeft="0.5dip"
                android:background="@drawable/layout_color"
                android:src="@mipmap/btn_4" />

            <ImageButton
                android:id="@+id/imageView5"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="0.5dip"
                android:layout_marginLeft="0.5dip"
                android:background="@drawable/layout_color"
                android:src="@mipmap/btn_5" />

            <ImageButton
                android:id="@+id/imageView6"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="0.5dip"
                android:layout_marginLeft="0.5dip"
                android:background="@drawable/layout_color"
                android:src="@mipmap/btn_6" />
        </TableRow>

        <TableRow
            android:background="@color/CAE"
            android:orientation="horizontal"
            android:paddingLeft="0.5dip"
            android:paddingRight="0.5dip"
            android:paddingTop="0dip" >

            <ImageButton
                android:id="@+id/imageView7"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="0.5dip"
                android:layout_marginLeft="0.5dip"
                android:background="@drawable/layout_color"
                android:src="@mipmap/btn_7" />

            <ImageButton
                android:id="@+id/imageView8"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="0.5dip"
                android:layout_marginLeft="0.5dip"
                android:background="@drawable/layout_color"
                android:src="@mipmap/btn_8" />

            <ImageButton
                android:id="@+id/imageView9"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="0.5dip"
                android:layout_marginLeft="0.5dip"
                android:background="@drawable/layout_color"
                android:src="@mipmap/btn_9" />
        </TableRow>

        <TableRow
            android:background="@color/CAE"
            android:orientation="horizontal"
            android:paddingLeft="0.5dip"
            android:paddingRight="0.5dip"
            android:paddingTop="0dip" >

            <ImageButton
                android:id="@+id/imageView_zl"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="0.5dip"
                android:layout_marginLeft="0.5dip"
                android:background="@drawable/layout_color"
                android:src="@mipmap/btn_zl" />

            <ImageButton
                android:id="@+id/imageView0"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="0.5dip"
                android:layout_marginLeft="0.5dip"
                android:background="@drawable/layout_color"
                android:src="@mipmap/btn_0" />

            <ImageButton
                android:id="@+id/imageView_zr"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="0.5dip"
                android:layout_marginLeft="0.5dip"
                android:background="@drawable/layout_color"
                android:src="@mipmap/btn_zr" />
        </TableRow>

    </TableLayout>

    <LinearLayout
        android:padding="10dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:src="@mipmap/dial_popdown_normal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <ImageView
            android:src="@mipmap/dial_callphone_normal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <ImageView
            android:src="@mipmap/dial_deletenumb_normal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <View
        android:background="@color/CAE"
        android:layout_width="match_parent"
        android:layout_height="1dp"></View>
</LinearLayout>

//第二步 EditText框我只要光标,不让它弹出软键盘:

我主要是用这个EditText框和拨号键盘配合使用

//3.0以上版本除了调用隐藏方法:setShowSoftInputOnFocus(false),
//由于是系统内部方法。无法直接调用,所以采用反射的方式来进行调用
//EditText框我只要光标,不让它弹出软键盘
if (android.os.Build.VERSION.SDK_INT <= 10) {
    mAddPhoneNumber.setInputType(InputType.TYPE_NULL);
} else {
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    try {
        Class<EditText> cls = EditText.class;
        Method setSoftInputShownOnFocus;
        setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
        setSoftInputShownOnFocus.setAccessible(true);
        setSoftInputShownOnFocus.invoke(mAddPhoneNumber, false);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//设置EditText是否显示光标,默认为显示:

mAddPhoneNumber.setCursorVisible(false);

//删除光标前的字符:

int index = mAddPhoneNumber.getSelectionStart();//获取到EditText光标
mAddPhoneNumber.getText().delete(index-1, index);//删除光标前的字符

//光标后添加字符 

int index = mAddPhoneNumber.getSelectionStart();//获取到EditText光标
mAddPhoneNumber.getText().insert(index, data);//光标后添加字符

//---------------------------------------------------------------------完----------------------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/weixin_42061754/article/details/83017120