最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android以對話框形式制作數(shù)字軟鍵盤示例

 更新時間:2021年12月14日 14:07:31   作者:矜月  
大家好,本篇文章主要講的是Android以對話框形式制作數(shù)字軟鍵盤示例,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽

一、介紹

Android的大部分自定義軟鍵盤主要是通過android自帶控件KeyboardView實現(xiàn)的。

那么,有沒有其他簡單易上手的方法來制作一個軟鍵盤呢?

當當當當!!!

這里就要說到對話框形式的軟鍵盤啦,簡單方便易理解!

下面,通過自定義的數(shù)字軟鍵盤來介紹如何利用對話框實現(xiàn)自定義軟鍵盤!

先看看效果!

二、布局編寫

我們先來看看布局!

首先是activity_main的實現(xiàn):

在這里,主界面布局非常簡單,僅需要兩個TextView文本框就可以啦!

注意!!!

為什么不是EditView控件來實現(xiàn)輸入框呢?

我們知道,TextView屬于文本控件,而EditView屬于輸入框控件,按理說咱們需要輸入內(nèi)容,應該選擇EditView輸入框控件。

但是!!!

點擊EditView輸入框是會自動跳出系統(tǒng)自帶的輸入法的。這樣的效果,會影響我們自定義軟鍵盤的彈出。所以我們還需要考慮如何禁用系統(tǒng)自動彈出的鍵盤。當然,這也不難實現(xiàn),只需要在onCreate中加上:
tv_shownumber.setFocusable(false);
就可以禁用系統(tǒng)鍵盤啦!

在這里,使用TextView文本框來實現(xiàn)數(shù)據(jù)輸入!

不啰嗦了!上主布局!

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" 的意思是點擊文本框之后會調(diào)用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之上的設置是非常重要的!

它使得我們整個鍵盤的寬度也發(fā)生了改變!

關于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);//動態(tài)添加布局
        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(); // 獲取對話框當前的參數(shù)值
        p.dimAmount = 0f;//設置背景透明度
        dialogWindow.setGravity(Gravity.BOTTOM);
        p.width =d.getWidth();//設置鍵盤的寬度
        dialogWindow.setAttributes(p);
        dialog.show();
    }
}

主代碼中為每個按鍵設置了功能,并引入了動態(tài)布局key.xml,實現(xiàn)了show()方法,讓自定義鍵盤以對話框的形式彈出!

在這里,背景透明度的設置可以使鍵盤的出現(xiàn)更加平滑,這一項的設置也可以在xml中進行哦!

dialogWindow.setGravity( Gravity.BOTTOM);

這句話的作用是讓我們鍵盤的位置位于頁面底部。

到此這篇關于Android以對話框形式制作數(shù)字軟鍵盤示例的文章就介紹到這了,更多相關Android制作數(shù)字軟鍵盤內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Android使用自定義字體的方法

    Android使用自定義字體的方法

    這篇文章主要介紹了Android使用自定義字體的方法,涉及Android字體設置的步驟與相關注意事項,需要的朋友可以參考下
    2016-01-01
  • Android編程實現(xiàn)從字符串中查找電話號碼的方法

    Android編程實現(xiàn)從字符串中查找電話號碼的方法

    這篇文章主要介紹了Android編程實現(xiàn)從字符串中查找電話號碼的方法,涉及Android針對字符串的匹配與查找相關技巧,需要的朋友可以參考下
    2016-03-03
  • Android利用滑動菜單框架實現(xiàn)滑動菜單效果

    Android利用滑動菜單框架實現(xiàn)滑動菜單效果

    這篇文章主要介紹了Android實現(xiàn)滑動菜單特效之滑動菜單框架完全解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Android實現(xiàn)縮放動畫

    Android實現(xiàn)縮放動畫

    這篇文章主要為大家詳細介紹了Android實現(xiàn)縮放動畫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • TextView長按復制的實現(xiàn)方法(總結)

    TextView長按復制的實現(xiàn)方法(總結)

    下面小編就為大家?guī)硪黄猅extView長按復制的實現(xiàn)方法(總結)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android中Glide加載庫的圖片緩存配置究極指南

    Android中Glide加載庫的圖片緩存配置究極指南

    這篇文章主要介紹了Android中Glide加載庫的圖片緩存配置究極指南,Glide是一款高人氣的安卓多媒體資源加載庫,本文對其緩存設置和優(yōu)化作了詳細講解,需要的朋友可以參考下
    2016-04-04
  • Android仿正點鬧鐘時間齒輪滑動效果

    Android仿正點鬧鐘時間齒輪滑動效果

    這篇文章主要為大家詳細介紹了Android仿正點鬧鐘時間齒輪滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Android獲得內(nèi)/外置存儲卡路徑的方法

    Android獲得內(nèi)/外置存儲卡路徑的方法

    我們知道Android上一般都有外置的存儲卡,內(nèi)置存儲卡路徑大家都知道怎么獲得的。那么如何獲取外置存儲卡的位置呢?下面小編通過本文給大家分享下
    2017-01-01
  • 基于自定義Toast全面解析

    基于自定義Toast全面解析

    下面小編就為大家?guī)硪黄谧远xToast全面解析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 最常見的猜拳小游戲Android代碼實現(xiàn)

    最常見的猜拳小游戲Android代碼實現(xiàn)

    這篇文章主要為大家詳細介紹了最常見的猜拳小游戲Android代碼實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評論

凤城市| 营山县| 鄂尔多斯市| 巩留县| 时尚| 孝义市| 保靖县| 吉安县| 乌拉特中旗| 海南省| 新巴尔虎右旗| 宝应县| 神农架林区| 三原县| 阜新市| 砀山县| 长寿区| 时尚| 金平| 新源县| 小金县| 榆中县| 宾川县| 南溪县| 广东省| 呼伦贝尔市| 灵璧县| 九龙城区| 肃北| 江口县| 利津县| 元江| 临武县| 平和县| 碌曲县| 泸州市| 阿尔山市| 密云县| 南靖县| 平塘县| 客服|