Android 系列 2.12使用KeyListener控制输入

263 篇文章 2 订阅
164 篇文章 0 订阅
2.12使用KeyListener控制输入


问题
您的应用程序包含要在其中限制用户仅输入数字的文本框; 此外,在某些情况下,您只希望允许使用正数,整数或日期。

Android提供了KeyListener类来帮助您限制用户只输入数字/正数/整数/正整数等等。
讨论
Android.text.method包包含一个KeyListener接口,以及一些类,例如DigitsKeyListener和DateKeyListener,它们实现了这个接口。
示例2-15是演示这些类中的一些类的示例应用程序。 此布局文件创建五个TextView和五个EditView; TextView显示它们各自的EditText允许的输入类型。

实施例2-15。 使用TextView和EditTexts布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview1"
android:text="digits listener with signs and decimal points"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview2"
android:text="digits listener without signs and decimal points"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview3"
android:text="date listener"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText3"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview4"
android:text="multitap listener"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText4"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview5"
android:text="qwerty listener"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText5"
/>
</LinearLayout>


示例2-16是将EditText输入限制为数字,正整数等(请参阅允许的键组的注释)的活动代码。
实施例2-16。 主要活动

import android.app.Activity;
import android.os.Bundle;
import android.text.method.DateKeyListener;
import android.text.method.DigitsKeyListener;
import android.text.method.MultiTapKeyListener;
import android.text.method.QwertyKeyListener;
import android.text.method.TextKeyListener;
import android.widget.EditText;
public class KeyListenerDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//allows digits with positive/negative signs and decimal points
EditText editText1=(EditText)findViewById(R.id.editText1);
DigitsKeyListener digkl1=DigitsKeyListener.getInstance(true,true);
editText1.setKeyListener(digkl1);
//allows positive integer only (no decimal values allowed)
EditText editText2=(EditText)findViewById(R.id.editText2);
DigitsKeyListener digkl2=DigitsKeyListener.getInstance();
editText2.setKeyListener(digkl2);
//allows date only
EditText editText3=(EditText)findViewById(R.id.editText3);
DateKeyListener dtkl=new DateKeyListener();
editText3.setKeyListener(dtkl);
//allows multitap with 12-key keypad layout
EditText editText4=(EditText)findViewById(R.id.editText4);
MultiTapKeyListener multitapkl =
new MultiTapKeyListener(TextKeyListener.Capitalize.WORDS,true);
editText4.setKeyListener(multitapkl);
//allows qwerty layout for typing
EditText editText5=(EditText)findViewById(R.id.editText5);
QwertyKeyListener qkl =
new QwertyKeyListener(TextKeyListener.Capitalize.SENTENCES,true);
editText5.setKeyListener(qkl);
}
}


要使用MultiTapKeyListener,您的手机应该支持12键布局,它需要激活。 要激活12键布局,请转到设置→语言和键盘→屏幕键盘布局,然后选择“电话布局”选项。


以下侦听器类型将用于编写此类型的应用程序。
BaseKeyListener这是一个关键侦听器的抽象基类。
DateTimeKeyListener用于在同一文本字段中输入日期和时间。
MetaKeyKeyListener此基类封装了用于跟踪元键(如SHIFT,ALT和SYM)状态以及选择文本的伪状态的行为。
NumberKeyListener这是用于数字文本输入。
TextKeyListener这是键入正常文本的关键侦听器。
TimeKeyListener这是用于在文本字段中输入时间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
KeyListenerJava 中的一个接口,用于监听键盘事件。它包含三个方法: 1. `keyPressed(KeyEvent e)`:按下键盘上的某个键时调用。 2. `keyReleased(KeyEvent e)`:释放键盘上的某个键时调用。 3. `keyTyped(KeyEvent e)`:敲击键盘上的某个键时调用。 下面是一个使用 KeyListener 的示例: ```java import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JPanel; public class KeyListenerExample extends JFrame implements KeyListener { public KeyListenerExample() { JPanel panel = new JPanel(); add(panel); addKeyListener(this); setSize(200, 200); setVisible(true); } public void keyPressed(KeyEvent e) { System.out.println("Key pressed: " + e.getKeyChar()); } public void keyReleased(KeyEvent e) { System.out.println("Key released: " + e.getKeyChar()); } public void keyTyped(KeyEvent e) { System.out.println("Key typed: " + e.getKeyChar()); } public static void main(String[] args) { new KeyListenerExample(); } } ``` 在这个示例中,我们创建了一个继承自 JFrame 的类,并实现了 KeyListener 接口。在构造方法中,我们创建了一个 JPanel,并将其添加到 JFrame 中。然后,通过 addKeyListener() 方法将 KeyListener 添加到 JFrame 中。最后,我们在三个方法中分别处理按下、释放和敲击键盘的事件,并输出相应的信息。 注意,在使用 KeyListener 时,需要将焦点设置到监听器所在的组件上,才能接收到键盘事件。在这个示例中,我们将监听器添加到 JFrame 中,因此需要将焦点设置到 JFrame 上才能接收到键盘事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值