为了账号安全,请及时绑定邮箱和手机立即绑定

android 自定义键盘

标签:
Android

刚刚做了个自定义键盘,方法记录下来以便以后用到参考,界面主要有一个editText和键盘。刚开始的时候我是用PopupWindow做的软键盘,后来发现不好用,因为PopupWindow和

editText不在一个层,每次点击键盘时EditText就没有焦点,

如果我输入了123456,现在想在2后面输入一个小数点,结果还要点击一下editText才能获得焦点,而此时popupWindow键盘会

消失,想输入只有再次点击editText后,popupWindow键盘才能显示出来,最后也没有找到解决的办法,所以我用了第二种方法比较简单。相对布局把键盘固定在最底部,别的不多说,先上图再上代码吧。

一、这是效果图,比较丑。。。

5be301c00001836902000355.jpg

二、这个是自定义键盘的xml文档,comm_keyboard.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" >
    <LinearLayout
        android:id="@+id/keyboard_layout"
        android:layout_width="match_parent"
        android:layout_height="225dip"
        android:layout_alignParentBottom="true"
        android:background="#f6f6f7"
        android:orientation="horizontal" >
        <TableLayout
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" >
            <TableRow>
                <Button
                    android:id="@+id/custom_keyboard_btn1"
                    android:layout_width="0dip"
                    android:layout_height="55dip"
                    android:layout_weight="1"
                    android:background="@drawable/table_line_gray"
                    android:gravity="center"
                    android:text="1"
                    android:textColor="#000000"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/custom_keyboard_btn2"
                    android:layout_width="0dip"
                    android:layout_height="55dip"
                    android:layout_weight="1"
                    android:background="@drawable/table_line_gray"
                    android:gravity="center"
                    android:text="2"
                    android:textColor="#000000"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/custom_keyboard_btn3"
                    android:layout_width="0dip"
                    android:layout_height="55dip"
                    android:layout_weight="1"
                    android:background="@drawable/table_line_gray"
                    android:gravity="center"
                    android:text="3"
                    android:textColor="#000000"
                    android:textSize="30sp" />
            </TableRow>
            <TableRow>
                <Button
                    android:id="@+id/custom_keyboard_btn4"
                    android:layout_width="0dip"
                    android:layout_height="55dip"
                    android:layout_weight="1"
                    android:background="@drawable/table_line_gray"
                    android:gravity="center"
                    android:text="4"
                    android:textColor="#000000"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/custom_keyboard_btn5"
                    android:layout_width="0dip"
                    android:layout_height="55dip"
                    android:layout_weight="1"
                    android:background="@drawable/table_line_gray"
                    android:gravity="center"
                    android:text="5"
                    android:textColor="#000000"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/custom_keyboard_btn6"
                    android:layout_width="0dip"
                    android:layout_height="55dip"
                    android:layout_weight="1"
                    android:background="@drawable/table_line_gray"
                    android:gravity="center"
                    android:text="6"
                    android:textColor="#000000"
                    android:textSize="30sp" />
            </TableRow>
            <TableRow>
                <Button
                    android:id="@+id/custom_keyboard_btn7"
                    android:layout_width="0dip"
                    android:layout_height="55dip"
                    android:layout_weight="1"
                    android:background="@drawable/table_line_gray"
                    android:gravity="center"
                    android:text="7"
                    android:textColor="#000000"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/custom_keyboard_btn8"
                    android:layout_width="0dip"
                    android:layout_height="55dip"
                    android:layout_weight="1"
                    android:background="@drawable/table_line_gray"
                    android:gravity="center"
                    android:text="8"
                    android:textColor="#000000"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/custom_keyboard_btn9"
                    android:layout_width="0dip"
                    android:layout_height="55dip"
                    android:layout_weight="1"
                    android:background="@drawable/table_line_gray"
                    android:gravity="center"
                    android:text="9"
                    android:textColor="#000000"
                    android:textSize="30sp" />
            </TableRow>
            <TableRow>
                <Button
                    android:id="@+id/custom_keyboard_btn_point"
                    android:layout_width="0dip"
                    android:layout_height="59dip"
                    android:layout_weight="1"
                    android:background="@drawable/table_line_gray"
                    android:gravity="center"
                    android:text="."
                    android:textColor="#000000"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/custom_keyboard_btn0"
                    android:layout_width="0dip"
                    android:layout_height="59dip"
                    android:layout_weight="1"
                    android:background="@drawable/table_line_gray"
                    android:gravity="center"
                    android:text="0"
                    android:textColor="#000000"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/custom_keyboard_btn_system_keyboard"
                    android:layout_width="0dip"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/table_line_gray"
                    android:gravity="center"
                    android:text="系统键"
                    android:textColor="#000000"
                    android:textSize="15sp" />
            </TableRow>
        </TableLayout>
        <LinearLayout
            android:layout_width="90dip"
            android:layout_height="match_parent"
            android:orientation="vertical" >
            <Button
                android:id="@+id/custom_keyboard_btn_delete"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:background="@drawable/table_line_gray"
                android:gravity="center"
                android:text="删除"
                android:textColor="#000000"
                android:textSize="20sp" />
            <Button
                android:id="@+id/custom_keyboard_btn_sure"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:background="@drawable/table_line_gray"
                android:gravity="center"
                android:text="确定"
                android:textSize="20sp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

这是activity_main.xml文档

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/input_lay"
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:gravity="center_vertical"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="金额:" />
        <EditText
            android:id="@+id/ed_write_money"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入金额" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/bottom_lay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
        <include layout="@layout/comm_keyboard" />
    </LinearLayout>
</RelativeLayout>

这是自定义键盘Activity,,CustomKeyboardActivity

package com.example.customkeyboard;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
/**
 * 自定义键盘
 * 
 */
public class CustomKeyboardActivity extends Activity implements OnFocusChangeListener
{
    private static final String TAG = CustomKeyboardActivity.class.getName();
    public static PopupWindow popupWindow = null;
    private String str = "";
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        initView();
        initData();
    }
    protected void initData()
    {
    }
    protected void initView()
    {
    }
    /**
     * 显示公共的自定义键盘
     * 
     * @param context
     * @param ed
     */
    public void showKeyBoard(Context context, final EditText ed)
    {
        try
        {
            Button btn0 = (Button) findViewById(R.id.custom_keyboard_btn0);
            Button btn1 = (Button) findViewById(R.id.custom_keyboard_btn1);
            Button btn2 = (Button) findViewById(R.id.custom_keyboard_btn2);
            Button btn3 = (Button) findViewById(R.id.custom_keyboard_btn3);
            Button btn4 = (Button) findViewById(R.id.custom_keyboard_btn4);
            Button btn5 = (Button) findViewById(R.id.custom_keyboard_btn5);
            Button btn6 = (Button) findViewById(R.id.custom_keyboard_btn6);
            Button btn7 = (Button) findViewById(R.id.custom_keyboard_btn7);
            Button btn8 = (Button) findViewById(R.id.custom_keyboard_btn8);
            Button btn9 = (Button) findViewById(R.id.custom_keyboard_btn9);
            Button pointBtn = (Button) findViewById(R.id.custom_keyboard_btn_point);
            Button system_broad_btn = (Button) findViewById(R.id.custom_keyboard_btn_system_keyboard);
            Button deleteBtn = (Button) findViewById(R.id.custom_keyboard_btn_delete);
            Button sureBtn = (Button) findViewById(R.id.custom_keyboard_btn_sure);
            btn0.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int index = ed.getSelectionStart();// 获得光标位置
                    Editable editable = ed.getText();
                    editable.insert(index, "0");
                }
            });
            btn1.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int index = ed.getSelectionStart();
                    Editable editable = ed.getText();
                    editable.insert(index, "1");
                }
            });
            btn2.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int index = ed.getSelectionStart();
                    Editable editable = ed.getText();
                    editable.insert(index, "2");
                }
            });
            btn3.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int index = ed.getSelectionStart();
                    Editable editable = ed.getText();
                    editable.insert(index, "3");
                }
            });
            btn4.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int index = ed.getSelectionStart();
                    Editable editable = ed.getText();
                    editable.insert(index, "4");
                }
            });
            btn5.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int index = ed.getSelectionStart();
                    Editable editable = ed.getText();
                    editable.insert(index, "5");
                }
            });
            btn6.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int index = ed.getSelectionStart();
                    Editable editable = ed.getText();
                    editable.insert(index, "6");
                }
            });
            btn7.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int index = ed.getSelectionStart();
                    Editable editable = ed.getText();
                    editable.insert(index, "7");
                }
            });
            btn8.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int index = ed.getSelectionStart();
                    Editable editable = ed.getText();
                    editable.insert(index, "8");
                }
            });
            btn9.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int index = ed.getSelectionStart();
                    Editable editable = ed.getText();
                    editable.insert(index, "9");
                }
            });
            pointBtn.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int index = ed.getSelectionStart();
                    Editable editable = ed.getText();
                    editable.insert(index, ".");
                }
            });
            deleteBtn.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    str = ed.getText().toString();
                    if (str.length() > 0)
                    {
                        int index = ed.getSelectionStart();// 获取光标位置
                        Editable editable = ed.getText();
                        editable.delete(index - 1, index);
                    }
                }
            });
            system_broad_btn.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Log.d(TAG, "系统键");
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
                }
            });
            sureBtn.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Log.d(TAG, "确定");
                }
            });
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    @Override
    public void onFocusChange(View v, boolean hasFocus)
    {
    }
}
这是MainActivity,继承CustomKeyboardActivity 
package com.example.customkeyboard;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends CustomKeyboardActivity implements OnClickListener
{
    private RelativeLayout parent = null;
    private EditText inputMoney;
    private LinearLayout inputLayout;
    @Override
    protected void initView()
    {
        setContentView(R.layout.activity_main);
        parent = (RelativeLayout) findViewById(R.id.main);
        inputLayout = (LinearLayout) findViewById(R.id.input_lay);
        inputMoney = (EditText) findViewById(R.id.ed_write_money);
        inputLayout = (LinearLayout) findViewById(R.id.input_lay);
        inputMoney.setOnClickListener(this);
        showKeyBoard(MainActivity.this, inputMoney);
    }
    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ed_write_money:
                // ShowPopupWindow(MainActivity.this, inputMoney, inputLayout);
                break;
        }
    }
}

原文链接:http://www.apkbus.com/blog-460955-59316.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消