zoukankan      html  css  js  c++  java
  • 键盘事件OnKeyListener

    1.效果图:就是在文本框中输入A,则显示A,输入B,则显示B....

    (1)activity_main.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.example.app3.MainActivity"
    11     android:orientation="vertical">
    12 
    13     <EditText
    14         android:id="@+id/et"
    15         android:layout_width="match_parent"
    16         android:layout_height="wrap_content" />
    17     <TextView
    18         android:id="@+id/tv_show"
    19         android:layout_width="wrap_content"
    20         android:layout_height="wrap_content"
    21         android:text="" />
    22 </LinearLayout>
    View Code

     (2)MainAcivity.java

     1 package com.example.app3;
     2 
     3 import android.content.DialogInterface;
     4 import android.support.v7.app.AppCompatActivity;
     5 import android.os.Bundle;
     6 import android.view.KeyEvent;
     7 import android.view.View;
     8 import android.widget.EditText;
     9 import android.widget.TextView;
    10 
    11 public class MainActivity extends AppCompatActivity {
    12     private EditText editText;
    13     private TextView textView;
    14 
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.activity_main);
    19 
    20         editText = (EditText) findViewById(R.id.et);
    21         textView = (TextView) findViewById(R.id.tv_show);
    22 
    23         editText.setOnKeyListener(new View.OnKeyListener() {
    24             @Override
    25             public boolean onKey(View v, int keyCode, KeyEvent event) {         //注意这里参数的变化
    26                 switch (event.getKeyCode()){
    27                     case KeyEvent.KEYCODE_A:
    28                         textView.setText("点击了A");
    29                         break;
    30                     case KeyEvent.KEYCODE_B:
    31                         textView.setText("点击了B");
    32                         break;
    33                     case KeyEvent.KEYCODE_C:
    34                         textView.setText("点击了C");
    35                         break;
    36                 }
    37                 return false;
    38             }
    39         });
    40 
    41     }
    42 }

     2.效果图:实现输入银行卡号,输入一个数,显示一个数,直到输出所有的银行卡号,四位显示一个空格

    (1)activity_main.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.example.app3.MainActivity"
    11     android:orientation="vertical">
    12 
    13     <EditText
    14         android:id="@+id/et"
    15         android:hint="请输入您的银行卡号"
    16         android:layout_width="match_parent"
    17         android:layout_height="wrap_content" />
    18     <TextView
    19         android:id="@+id/tv_show"
    20         android:layout_width="wrap_content"
    21         android:layout_height="wrap_content"
    22         android:text="" />
    23 </LinearLayout>

     (2)MainAcivity.java

     1 package com.example.app3;
     2 
     3 import android.content.DialogInterface;
     4 import android.support.v7.app.AppCompatActivity;
     5 import android.os.Bundle;
     6 import android.view.KeyEvent;
     7 import android.view.View;
     8 import android.widget.EditText;
     9 import android.widget.TextView;
    10 
    11 public class MainActivity extends AppCompatActivity {
    12     private EditText editText;
    13     private TextView textView;
    14 
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.activity_main);
    19 
    20         editText = (EditText) findViewById(R.id.et);
    21         textView = (TextView) findViewById(R.id.tv_show);
    22 
    23         editText.setOnKeyListener(new View.OnKeyListener() {
    24             @Override
    25             public boolean onKey(View v, int keyCode, KeyEvent event) {
    26             editText.setOnKeyListener(new View.OnKeyListener() {
    27                 @Override
    28                 public boolean onKey(View v, int keyCode, KeyEvent event) {
    29                     switch (event.getAction()){
    30                         case KeyEvent.ACTION_UP:
    31                             String info = editText.getText().toString();
    32                             textView.setText("您输入的银行卡号是:"+subs(info));
    33                         case  KeyEvent.ACTION_DOWN:
    34                             break;
    35                     }
    36                     return false;
    37                 }
    38             });
    39                 return false;
    40             }
    41         });
    42 
    43     }
    44 
    45     public String subs(String info){
    46         String mess="";
    47         for(int i =0;i<=info.length()/4;i++){
    48             if(i*4+4<info.length()){
    49                 mess=mess+info.substring(i*4,i*4+4)+"  ";
    50             }else{
    51                 mess=mess+info.substring(i*4,info.length());
    52             }
    53         }
    54         return mess;
    55     }
    56 }
  • 相关阅读:
    Tomcat下HTTPS双向认证配置以及客户端调用案例
    Java本地运行中文正常,部署到Weblogic中文乱码
    gson 忽略掉某些字段不进行转换
    JavaScript中定义对象的四种方式
    使用CSS3实现超炫的Loading(加载)动画效果
    不要再使用JS框架了
    HTML5, CSS3, ES5新的web标准和浏览器支持一览 转
    js常用的事件对象
    jQuery用面向对象的思想来编写验证表单的插件
    五个值得尝试的前端开发工具
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/9002594.html
Copyright © 2011-2022 走看看