一、介绍

Android的大部分自定义软键盘主要是通过android自带控件KeyboardView实现的。

那么,有没有其他简单易上手的方法来制作一个软键盘呢?

当当当当!!!

这里就要说到对话框形式的软键盘啦,简单方便易理解!

下面,通过自定义的数字软键盘来介绍如何利用对话框实现自定义软键盘!

先看看效果!

二、布局编写

我们先来看看布局!

首先是activity_main的实现:

在这里,主界面布局非常简单,仅需要两个TextView文本框就可以啦!

注意!!!

为什么不是EditView控件来实现输入框呢?

我们知道,TextView属于文本控件,而EditView属于输入框控件,按理说咱们需要输入内容,应该选择EditView输入框控件。

但是!!!

点击EditView输入框是会自动跳出系统自带的输入法的。这样的效果,会影响我们自定义软键盘的弹出。所以我们还需要考虑如何禁用系统自动弹出的键盘。当然,这也不难实现,只需要在onCreate中加上:
tv_shownumber.setFocusable(false);
就可以禁用系统键盘啦!

在这里,使用TextView文本框来实现数据输入!

不啰嗦了!上主布局!

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#C3F4C5">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:text="身份证:"
            android:gravity="left|center"
            android:textColor="#000000"
            android:textSize="25sp"/>
        <TextView
            android:id="@ id/tv_shownumber"
            android:onClick="show"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="  请输入身份证号"
            android:background="#FFFFFF"
            android:gravity="left|center"
            android:textColor="#000000"
            android:maxLength="18"
            android:textSize="25sp"
            android:textColorHint="#B6B1B1"/>
    </LinearLayout>
</LinearLayout>

android:onClick="show" 的意思是点击文本框之后会调用show这个方法!

接下来是自定义软键盘布局!

key.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="bottom">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp">
        <TextView
            android:id="@ id/tv_show"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="50dp"
            android:textColor="#000000"
            android:textSize="20sp"
            android:maxLength="18"
            android:gravity="center"
            android:maxLines="18"
            android:background="@drawable/keyboard_btn"
            />
        <Button
            android:id="@ id/btn_yes"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="完成"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="25dp"
            android:layout_margin="3dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@ id/btn_1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="1"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="40dp"
            android:layout_margin="3dp"/>
        <Button
            android:id="@ id/btn_2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="40dp"
            android:layout_margin="3dp"/>
        <Button
            android:id="@ id/btn_3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="3"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="40dp"
            android:layout_margin="3dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@ id/btn_4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="4"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="40dp"
            android:layout_margin="3dp"/>
        <Button
            android:id="@ id/btn_5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="5"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="40dp"
            android:layout_margin="3dp"/>
        <Button
            android:id="@ id/btn_6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="6"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="40dp"
            android:layout_margin="3dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@ id/btn_7"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="7"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="40dp"
            android:layout_margin="3dp"/>
        <Button
            android:id="@ id/btn_8"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="8"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="40dp"
            android:layout_margin="3dp"/>
        <Button
            android:id="@ id/btn_9"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="9"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="40dp"
            android:layout_margin="3dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@ id/btn_0"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="0"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="40dp"
            android:layout_margin="3dp"/>
        <Button
            android:id="@ id/btn_X"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="X"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="40dp"
            android:layout_margin="3dp"/>
        <Button
            android:id="@ id/btn_del"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="←"
            android:background="@drawable/keyboard_btn"
            android:textStyle="bold"
            android:textSize="40dp"
            android:layout_margin="3dp"/>
    </LinearLayout>
</LinearLayout>

代码很长,但总结起来就是13个按键加上一个TextView文本框。这样一想是不是瞬间觉得毫无难度啦!

布局代码到这里就完成了!
如果你想让你的键盘看起来好看一些,点击有颜色变化效果,也可以给它设置样式!

这是按钮没有皮肤的样子:

这是按钮有皮肤的样子:

差别还是挺大的!

下面附上按钮样式的代码:

keyboard_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <!--圆角大小-->
            <corners
                android:radius="4dp"/>
            <!--距离-->
            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp"/>
            <!--点击边框颜色-->
            <stroke
                android:color="#81CDEF"
                android:width="4dp"/>
            <!--点击填充颜色-->
            <solid android:color="#AAE2FB"/>
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape>
            <corners
                android:radius="4dp"/>
            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp"/>
        </shape>
    </item>
    <item>
        <shape>
            <corners
                android:radius="4dp"/>
            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp"/>
            <solid android:color="#ffffff"/>
            <stroke
                android:width="4dp"
                android:color="#A5DCA7"/>
        </shape>
    </item>
</selector>

当然,不仅键盘按钮需要皮肤,键盘本身也需要!

这是键盘没有皮肤的样子:

这是键盘有皮肤的样子:

下面是键盘样式的代码:

DialogStyle.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="DialogStyle"
        android:parent="@android:style/Theme.Dialog"
        xmlns:android="http://schemas.android.com/apk/res/android">
 
        <!--&lt;!&ndash; 背景颜色 &ndash;&gt;-->
        <item name="android:windowBackground">#AFE1B1</item>
        <item name="android:windowContentOverlay">@null</item>
 
        <!-- 浮于Activity之上 -->
        <item name="android:windowIsFloating">true</item>
 
        <!--&lt;!&ndash; 边框 &ndash;&gt;-->
        <item name="android:windowFrame">@null</item>
    </style>
</resources>

在这里,浮于Activity之上的设置是非常重要的!

它使得我们整个键盘的宽度也发生了改变!

关于xml文件的存放位置,我们一般将activity_main.xml和key.xml放在layout文件夹下,将keyboard_btn.xml放在drawable文件夹下,DialogStyle.xml放在values文件夹下。

三、逻辑编写

有了好看的皮囊,自然也需要有趣的灵魂!

它来了它来了!

MainActivity

package com.example.mydialog;
 
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
 
import android.app.Dialog;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tv_shownumber;
//    private EditText tv_shownumber;
    private String shownumber;
    private Dialog dialog;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_shownumber = findViewById(R.id.tv_shownumber);
    }
 
    private String num[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9","X"};
    private String data = "";
    @Override
    public void onClick(View v) {
        shownumber  = tv_shownumber.getText().toString();
        switch (v.getId()){
            case R.id.btn_0:
                data  = num[0];
                tv_shownumber.setText(data);
                tv_show.setText(data);
                break;
            case R.id.btn_1:
                data  = num[1];
                tv_shownumber.setText(data);
                tv_show.setText(data);
                break;
            case R.id.btn_2:
                data  = num[2];
                tv_shownumber.setText(data);
                tv_show.setText(data);
                break;
            case R.id.btn_3:
                data  = num[3];
                tv_shownumber.setText(data);
                tv_show.setText(data);
                break;
            case R.id.btn_4:
                data  = num[4];
                tv_shownumber.setText(data);
                tv_show.setText(data);
                break;
            case R.id.btn_5:
                data  = num[5];
                tv_shownumber.setText(data);
                tv_show.setText(data);
                break;
            case R.id.btn_6:
                data  = num[6];
                tv_shownumber.setText(data);
                tv_show.setText(data);
                break;
            case R.id.btn_7:
                data  = num[7];
                tv_shownumber.setText(data);
                tv_show.setText(data);
                break;
            case R.id.btn_8:
                data  = num[8];
                tv_shownumber.setText(data);
                tv_show.setText(data);
                break;
            case R.id.btn_9:
                data  = num[9];
                tv_shownumber.setText(data);
                tv_show.setText(data);
                break;
            case R.id.btn_X:
                data  = num[10];
                tv_shownumber.setText(data);
                tv_show.setText(data);
                break;
            case R.id.btn_yes:
                dialog.dismiss();
                break;
            case R.id.btn_del:
                data = data.substring(0, data.length() - 1);
                tv_shownumber.setText(data);
                tv_show.setText(data);
                break;
            default:
                break;
        }
    }
 
    private View inflate;
    private Button btn_0;
    private Button btn_1;
    private Button btn_2;
    private Button btn_3;
    private Button btn_4;
    private Button btn_5;
    private Button btn_6;
    private Button btn_7;
    private Button btn_8;
    private Button btn_9;
    private Button btn_X;
    private Button btn_yes;
    private Button btn_del;
    private TextView tv_show;
 
    public void show(View view){
        dialog =new Dialog(this,R.style.DialogStyle);
        inflate = LayoutInflater.from(this).inflate(R.layout.key, null);//动态添加布局
        btn_0 = inflate.findViewById(R.id.btn_0);
        btn_1 = inflate.findViewById(R.id.btn_1);
        btn_2 = inflate.findViewById(R.id.btn_2);
        btn_3 = inflate.findViewById(R.id.btn_3);
        btn_4 = inflate.findViewById(R.id.btn_4);
        btn_5 = inflate.findViewById(R.id.btn_5);
        btn_6 = inflate.findViewById(R.id.btn_6);
        btn_7 = inflate.findViewById(R.id.btn_7);
        btn_8 = inflate.findViewById(R.id.btn_8);
        btn_9 = inflate.findViewById(R.id.btn_9);
        btn_X = inflate.findViewById(R.id.btn_X);
        btn_yes = inflate.findViewById(R.id.btn_yes);
        btn_del = inflate.findViewById(R.id.btn_del);
        tv_show = inflate.findViewById(R.id.tv_show);
 
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_X.setOnClickListener(this);
        btn_0.setOnClickListener(this);
        btn_yes.setOnClickListener(this);
        btn_del.setOnClickListener(this);
 
        dialog.setContentView(inflate);
        Window dialogWindow = dialog.getWindow();
 
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.dimAmount = 0f;//设置背景透明度
        dialogWindow.setGravity(Gravity.BOTTOM);
        p.width =d.getWidth();//设置键盘的宽度
        dialogWindow.setAttributes(p);
        dialog.show();
    }
}

主代码中为每个按键设置了功能,并引入了动态布局key.xml,实现了show()方法,让自定义键盘以对话框的形式弹出!

在这里,背景透明度的设置可以使键盘的出现更加平滑,这一项的设置也可以在xml中进行哦!

dialogWindow.setGravity( Gravity.BOTTOM);

这句话的作用是让我们键盘的位置位于页面底部。

到此这篇关于Android以对话框形式制作数字软键盘示例的文章就介绍到这了,更多相关Android制作数字软键盘内容请搜索Devmax以前的文章或继续浏览下面的相关文章希望大家以后多多支持Devmax!

Android以对话框形式制作数字软键盘示例的更多相关文章

  1. html5 canvas合成海报所遇问题及解决方案总结

    这篇文章主要介绍了html5 canvas合成海报所遇问题及解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Html5 video标签视频的最佳实践

    这篇文章主要介绍了Html5 video标签视频的最佳实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  3. HTML5在微信内置浏览器下右上角菜单的调整字体导致页面显示错乱的问题

    HTML5在微信内置浏览器下,在右上角菜单的调整字体导致页面显示错乱的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

  4. ios – containerURLForSecurityApplicationGroupIdentifier:在iPhone和Watch模拟器上给出不同的结果

    我使用默认的XCode模板创建了一个WatchKit应用程序.我向iOSTarget,WatchkitAppTarget和WatchkitAppExtensionTarget添加了应用程序组权利.(这是应用程序组名称:group.com.lombax.fiveminutes)然后,我尝试使用iOSApp和WatchKitExtension访问共享文件夹URL:延期:iOS应用:但是,测试NSURL

  5. Ionic – Splash Screen适用于iOS,但不适用于Android

    我有一个离子应用程序,其中使用CLI命令离子资源生成的启动画面和图标iOS版本与正在渲染的启动画面完美配合,但在Android版本中,只有在加载应用程序时才会显示白屏.我检查了config.xml文件,所有路径看起来都是正确的,生成的图像出现在相应的文件夹中.(我使用了splash.psd模板来生成它们.我错过了什么?这是config.xml文件供参考,我觉得我在这里做错了–解决方法在config.xml中添加以下键:它对我有用!

  6. ios – 无法启动iPhone模拟器

    /Library/Developer/CoreSimulator/Devices/530A44CB-5978-4926-9E91-E9DBD5BFB105/data/Containers/Bundle/Application/07612A5C-659D-4C04-ACD3-D211D2830E17/ProductName.app/ProductName然后,如果您在Xcode构建设置中选择标准体系结构并再次构建和运行,则会产生以下结果:dyld:lazysymbolbindingFailed:Symbol

  7. Xamarin iOS图像在Grid内部重叠

    heyo,所以在Xamarin我有一个使用并在其中包含一对,所有这些都包含在内.这在Xamarin.Android中看起来完全没问题,但是在Xamarin.iOS中,图像与标签重叠.我不确定它的区别是什么–为什么它在Xamarin.Android中看起来不错但在iOS中它的全部都不稳定?

  8. 在iOS上向后播放HTML5视频

    我试图在iPad上反向播放HTML5视频.HTML5元素包括一个名为playbackRate的属性,它允许以更快或更慢的速率或相反的方式播放视频.根据Apple’sdocumentation,iOS不支持此属性.通过每秒多次设置currentTime属性,可以反复播放,而无需使用playbackRate.这种方法适用于桌面Safari,但似乎在iOS设备上的搜索限制为每秒1次更新–在我的情况下太慢了.有没有办法在iOS设备上向后播放HTML5视频?解决方法iOS6Safari现在支持playbackRat

  9. Swift开发教程--UITextField输入框如何隐藏软键盘

    对于UITextField如何隐藏输入框,一直是初学者常遇到的问题。在View试图中,点击选中UITextField控件,为其添加DidEndOnExit实践。在弹出软键盘之后,点击return就可以隐藏软键盘了。

  10. 使用 Swift 语言编写 Android 应用入门

    Swift标准库可以编译安卓armv7的内核,这使得可以在安卓移动设备上执行Swift语句代码。做梦,虽然Swift编译器可以胜任在安卓设备上编译Swift代码并运行。这需要的不仅仅是用Swift标准库编写一个APP,更多的是你需要一些框架来搭建你的应用用户界面,以上这些Swift标准库不能提供。简单来说,构建在安卓设备上使用的Swiftstdlib需要libiconv和libicu。通过命令行执行以下命令:gitclonegit@github.com:SwiftAndroid/libiconv-libi

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部