1.效果图:
(1)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 }
2.acivity_mian.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>