├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── xylitolz │ │ └── androidverificationcode │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── xylitolz │ │ │ └── androidverificationcode │ │ │ ├── MainActivity.java │ │ │ └── view │ │ │ ├── VerificationCodeView.java │ │ │ └── WiseEditText.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── bg_text_focused.xml │ │ ├── bg_text_normal.xml │ │ ├── cursor.xml │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── xylitolz │ └── androidverificationcode │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── image └── 自定义View-验证码仿滴滴.gif └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riceeater/AndroidVerificationCode/c05876dfe5a484a68f90accd72cdb00bb49db928/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 类滴滴验证码输入框,长这样: 2 | 3 | ![](image/自定义View-验证码仿滴滴.gif) 4 | 5 | 6 | 7 | ### 实现思路 8 | 9 | **笔者这里决定继承RelativeLayout来实现自定义验证码的功能。显示验证码可以使用几个TextView,这里需要将TextView统一管理,所以还需要一个TextView数组。有光标,那自然而然的就想到了EditText,可以使用一个透明背景的EditText。几个验证码可以使用TextView以一定的规则进行排列,通过监听EditText的输入,拦截到输入字符,并将字符传递给TextView数组,并将EditText置为空,同时重设TextView选中状态,移动EditText光标。笔者这里采用给EditText设置paddingLeft的方式来实现光标的移动,当然,需要经过一些计算。OK,大概思路就是这样了,具体的代码我们继续看下面的。** 10 | 11 | ### 使用: 12 | 13 | ```xml 14 | 25 | ``` 26 | 27 | 28 | 29 | 效果参考上图 30 | 31 | 32 | 33 | 相关博客地址:[Android自定义View-仿滴滴自定义验证码输入框](https://riceeater.gitee.io/post/23-VerificationCodeView) 34 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.xylitolz.androidverificationcode" 7 | minSdkVersion 14 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:27.1.1' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.0' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | } 29 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/xylitolz/androidverificationcode/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.xylitolz.androidverificationcode; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.xylitolz.androidverificationcode", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/xylitolz/androidverificationcode/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.xylitolz.androidverificationcode; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | import android.widget.Button; 7 | import android.widget.Toast; 8 | 9 | import com.xylitolz.androidverificationcode.view.VerificationCodeView; 10 | 11 | public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 12 | private VerificationCodeView viewVerification; 13 | private Button btnSubmit; 14 | private Button btnClear; 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(R.layout.activity_main); 19 | viewVerification = findViewById(R.id.view_verification); 20 | btnSubmit = findViewById(R.id.btn_submit); 21 | btnClear = findViewById(R.id.btn_clear); 22 | 23 | btnSubmit.setOnClickListener(this); 24 | btnClear.setOnClickListener(this); 25 | } 26 | 27 | 28 | @Override 29 | public void onClick(View v) { 30 | switch (v.getId()) { 31 | case R.id.btn_submit: 32 | if(viewVerification.isFinish()) { 33 | Toast.makeText(this,"输入验证码是:"+viewVerification.getContent(),Toast.LENGTH_SHORT).show(); 34 | } else { 35 | Toast.makeText(this,"请输入完整验证码",Toast.LENGTH_SHORT).show(); 36 | } 37 | break; 38 | case R.id.btn_clear: 39 | viewVerification.clear(); 40 | break; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/xylitolz/androidverificationcode/view/VerificationCodeView.java: -------------------------------------------------------------------------------- 1 | package com.xylitolz.androidverificationcode.view; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Color; 6 | import android.graphics.drawable.Drawable; 7 | import android.support.annotation.DrawableRes; 8 | import android.text.Editable; 9 | import android.text.InputType; 10 | import android.text.TextUtils; 11 | import android.text.TextWatcher; 12 | import android.util.AttributeSet; 13 | import android.util.TypedValue; 14 | import android.view.Gravity; 15 | import android.view.KeyEvent; 16 | import android.view.View; 17 | import android.view.ViewGroup; 18 | import android.widget.RelativeLayout; 19 | import android.widget.TextView; 20 | 21 | import com.xylitolz.androidverificationcode.R; 22 | 23 | /** 24 | * @author 小米Xylitol 25 | * @email xiaomi987@hotmail.com 26 | * @desc Android验证码View 27 | * @date 2018-05-09 10:25 28 | */ 29 | public class VerificationCodeView extends RelativeLayout { 30 | 31 | //当前验证码View用来展示验证码的TextView数组,数组个数由codeNum决定 32 | private TextView[] textViews; 33 | //用来输入验证码的EditText输入框,输入框会跟随输入个数移动,显示或者隐藏光标等 34 | private WiseEditText editText; 35 | //当前验证码View展示验证码个数 36 | private int codeNum; 37 | //每个TextView的宽度 38 | private float codeWidth; 39 | //字体颜色 40 | private int textColor; 41 | //字体大小 42 | private int textSize; 43 | //每个单独验证码的背景 44 | private Drawable textDrawable; 45 | //验证码选中时的背景 46 | private Drawable textFocusedDrawable; 47 | //是否展示密码样式 48 | private boolean showPassword = false; 49 | //验证码之间间隔 50 | private float dividerWidth = 0; 51 | //对EditText输入进行监听 52 | private TextWatcher watcher; 53 | //监听删除键和enter键 54 | private OnKeyListener onKeyListener; 55 | 56 | //当前选中的TextView位置,即光标所在位置 57 | private int currentFocusPosition = 0; 58 | 59 | public VerificationCodeView(Context context) { 60 | this(context, null); 61 | } 62 | 63 | public VerificationCodeView(Context context, AttributeSet attrs) { 64 | this(context, attrs, 0); 65 | } 66 | 67 | public VerificationCodeView(Context context, AttributeSet attrs, int defStyleAttr) { 68 | super(context, attrs, defStyleAttr); 69 | init(context, attrs, defStyleAttr); 70 | } 71 | 72 | /** 73 | * 初始化View 74 | * 75 | * @param context 76 | * @param attrs 77 | * @param defStyleAttr 78 | */ 79 | private void init(Context context, AttributeSet attrs, int defStyleAttr) { 80 | TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.VerificationCodeView, defStyleAttr, 0); 81 | codeNum = array.getInteger(R.styleable.VerificationCodeView_vcv_code_number, 4); 82 | codeWidth = array.getDimensionPixelSize(R.styleable.VerificationCodeView_vcv_code_width, (int) dip2px(50, context)); 83 | textColor = array.getColor(R.styleable.VerificationCodeView_vcv_code_color, getResources().getColor(R.color.text_border_focused)); 84 | textSize = array.getDimensionPixelSize(R.styleable.VerificationCodeView_vcv_code_size, getResources().getDimensionPixelOffset(R.dimen.text_size)); 85 | textDrawable = array.getDrawable(R.styleable.VerificationCodeView_vcv_code_bg_normal); 86 | textFocusedDrawable = array.getDrawable(R.styleable.VerificationCodeView_vcv_code_bg_focus); 87 | showPassword = array.getBoolean(R.styleable.VerificationCodeView_vcv_code_input_style, false); 88 | array.recycle(); 89 | //若未设置选中和未选中状态,设置默认 90 | if (textDrawable == null) { 91 | textDrawable = getResources().getDrawable(R.drawable.bg_text_normal); 92 | } 93 | if (textFocusedDrawable == null) { 94 | textFocusedDrawable = getResources().getDrawable(R.drawable.bg_text_focused); 95 | } 96 | 97 | initView(context); 98 | initListener(); 99 | resetCursorPosition(); 100 | } 101 | 102 | /** 103 | * 初始化各View并加入当前View中 104 | */ 105 | private void initView(Context context) { 106 | //初始化TextView数组 107 | textViews = new TextView[codeNum]; 108 | for (int i = 0; i < codeNum; i++) { 109 | //循环加入数组中,设置TextView字体大小和颜色,并将TextView依次加入LinearLayout 110 | TextView textView = new TextView(context); 111 | textView.setGravity(Gravity.CENTER); 112 | textView.setTextColor(textColor); 113 | textView.setTextSize(textSize); 114 | textView.setIncludeFontPadding(false); 115 | if(showPassword) { 116 | //数字密码样式,可更改 117 | textView.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD); 118 | } 119 | textViews[i] = textView; 120 | this.addView(textView); 121 | RelativeLayout.LayoutParams params = (LayoutParams) textView.getLayoutParams(); 122 | params.addRule(CENTER_VERTICAL); 123 | params.width = (int) codeWidth; 124 | params.height = (int) codeWidth; 125 | } 126 | //初始化EditText,设置背景色为透明,获取焦点,设置光标颜色,设置输入类型等 127 | editText = new WiseEditText(context); 128 | editText.setBackgroundColor(Color.TRANSPARENT); 129 | editText.requestFocus(); 130 | editText.setInputType(InputType.TYPE_CLASS_NUMBER); 131 | setCursorRes(R.drawable.cursor); 132 | addView(editText, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 133 | } 134 | 135 | /** 136 | * 监听EditText输入字符,监听键盘删除键 137 | */ 138 | private void initListener() { 139 | watcher = new TextWatcher() { 140 | @Override 141 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { 142 | 143 | } 144 | 145 | @Override 146 | public void onTextChanged(CharSequence s, int start, int before, int count) { 147 | 148 | } 149 | 150 | @Override 151 | public void afterTextChanged(Editable s) { 152 | String content = s.toString(); 153 | if (!TextUtils.isEmpty(content)) { 154 | if (content.length() == 1) { 155 | setText(content); 156 | } 157 | editText.setText(""); 158 | } 159 | } 160 | }; 161 | editText.addTextChangedListener(watcher); 162 | 163 | onKeyListener = new OnKeyListener() { 164 | @Override 165 | public boolean onKey(View v, int keyCode, KeyEvent event) { 166 | if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) { 167 | deleteCode(); 168 | return true; 169 | } 170 | return false; 171 | } 172 | }; 173 | editText.setSoftKeyListener(onKeyListener); 174 | } 175 | 176 | /** 177 | * 重设EditText的光标位置,以及选中TextView的边框颜色 178 | */ 179 | private void resetCursorPosition() { 180 | for (int i = 0; i < codeNum; i++) { 181 | TextView textView = textViews[i]; 182 | if (i == currentFocusPosition) { 183 | textView.setBackgroundDrawable(textFocusedDrawable); 184 | } else { 185 | textView.setBackgroundDrawable(textDrawable); 186 | } 187 | } 188 | if (codeNum > 1) { 189 | if (currentFocusPosition < codeNum) { 190 | //字数小于总数,设置EditText的leftPadding,造成光标移动的错觉 191 | editText.setCursorVisible(true); 192 | float leftPadding = codeWidth / 2 + currentFocusPosition * codeWidth + currentFocusPosition * dividerWidth; 193 | editText.setPadding((int) leftPadding, 0, 0, 0); 194 | } else { 195 | //字数大于总数,隐藏光标 196 | editText.setCursorVisible(false); 197 | } 198 | } 199 | } 200 | 201 | /** 202 | * 删除键按下 203 | */ 204 | private void deleteCode() { 205 | if (currentFocusPosition == 0) { 206 | //当前光标位置在0,直接返回 207 | return; 208 | } else { 209 | //当前光标不为0,当前光标位置-1,将当前光标位置TextView置为"",重设光标位置 210 | currentFocusPosition--; 211 | textViews[currentFocusPosition].setText(""); 212 | resetCursorPosition(); 213 | } 214 | } 215 | 216 | /** 217 | * onMeasure后获取到测量的控件宽度,计算出每个Code之间的间隔 218 | */ 219 | private void layoutTextView() { 220 | //获取控件剩余宽度 221 | float availableWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); 222 | if (codeNum > 1) { 223 | //计算每个Code之间的间距 224 | dividerWidth = (availableWidth - codeWidth * codeNum) / (codeNum - 1); 225 | for (int i = 1; i < codeNum; i++) { 226 | float leftMargin = codeWidth * i + dividerWidth * i; 227 | RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) textViews[i].getLayoutParams(); 228 | params1.leftMargin = (int) leftMargin; 229 | } 230 | } 231 | 232 | //设置EditText宽度从第一个Code左侧到最后一个Code右侧,设置高度为Code高度 233 | //设置EditText为纵向居中 234 | editText.setWidth((int) availableWidth); 235 | editText.setHeight((int) codeWidth); 236 | RelativeLayout.LayoutParams params = (LayoutParams) editText.getLayoutParams(); 237 | params.addRule(CENTER_VERTICAL); 238 | } 239 | 240 | /** 241 | * 拦截到EditText输入字符,发送给该方法进行处理 242 | * 243 | * @param s 244 | */ 245 | private void setText(String s) { 246 | if (currentFocusPosition >= codeNum) { 247 | //光标已经隐藏,直接返回 248 | return; 249 | } 250 | //设置字符给当前光标位置的TextView,光标位置后移,重设光标状态 251 | textViews[currentFocusPosition].setText(s); 252 | currentFocusPosition++; 253 | resetCursorPosition(); 254 | } 255 | 256 | @Override 257 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 258 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); 259 | if (heightMode == MeasureSpec.AT_MOST) { 260 | //若高度是wrap_content,则设置为50dp 261 | heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) dip2px(80, getContext()), MeasureSpec.EXACTLY); 262 | } 263 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 264 | } 265 | 266 | @Override 267 | protected void onLayout(boolean changed, int l, int t, int r, int b) { 268 | super.onLayout(changed, l, t, r, b); 269 | //获取到宽高,可以对TextView进行摆放 270 | if(dividerWidth == 0) { 271 | layoutTextView(); 272 | } 273 | } 274 | 275 | private float dip2px(float dp, Context context) { 276 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 277 | dp, context.getResources().getDisplayMetrics()); 278 | } 279 | 280 | /** 281 | * 暴露公共方法,设置光标颜色 282 | * 283 | * @param drawableRes 284 | */ 285 | public void setCursorRes(@DrawableRes int drawableRes) { 286 | try { 287 | java.lang.reflect.Field f = TextView.class.getDeclaredField("mCursorDrawableRes"); 288 | f.setAccessible(true); 289 | f.set(editText, drawableRes); 290 | } catch (Exception e) { 291 | } 292 | } 293 | 294 | /** 295 | * 获取输入的验证码 296 | * 297 | * @return 298 | */ 299 | public String getContent() { 300 | StringBuilder builder = new StringBuilder(); 301 | for (TextView tv : textViews) { 302 | builder.append(tv.getText()); 303 | } 304 | return builder.toString(); 305 | } 306 | 307 | /** 308 | * 判断是否验证码输入完毕 309 | * 310 | * @return 311 | */ 312 | public boolean isFinish() { 313 | for (TextView tv : textViews) { 314 | if (TextUtils.isEmpty(tv.getText())) { 315 | return false; 316 | } 317 | } 318 | return true; 319 | } 320 | 321 | /** 322 | * 清除已输入验证码 323 | */ 324 | public void clear() { 325 | for (TextView tv : textViews) { 326 | tv.setText(""); 327 | } 328 | currentFocusPosition = 0; 329 | resetCursorPosition(); 330 | } 331 | } 332 | -------------------------------------------------------------------------------- /app/src/main/java/com/xylitolz/androidverificationcode/view/WiseEditText.java: -------------------------------------------------------------------------------- 1 | package com.xylitolz.androidverificationcode.view; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.AppCompatEditText; 5 | import android.util.AttributeSet; 6 | import android.view.KeyEvent; 7 | import android.view.inputmethod.EditorInfo; 8 | import android.view.inputmethod.InputConnection; 9 | import android.view.inputmethod.InputConnectionWrapper; 10 | 11 | /** 12 | * @author 小米Xylitol 13 | * @email xiaomi987@hotmail.com 14 | * @desc 15 | * @date 2018-05-09 14:56 16 | */ 17 | public class WiseEditText extends AppCompatEditText { 18 | 19 | 20 | private OnKeyListener keyListener; 21 | 22 | public WiseEditText(Context context, AttributeSet attrs, int defStyle) { 23 | super(context, attrs, defStyle); 24 | } 25 | 26 | public WiseEditText(Context context, AttributeSet attrs) { 27 | super(context, attrs); 28 | } 29 | 30 | public WiseEditText(Context context) { 31 | super(context); 32 | } 33 | 34 | @Override 35 | public InputConnection onCreateInputConnection(EditorInfo outAttrs) { 36 | return new MyInputConnection(super.onCreateInputConnection(outAttrs), 37 | true); 38 | } 39 | 40 | private class MyInputConnection extends InputConnectionWrapper { 41 | 42 | public MyInputConnection(InputConnection target, boolean mutable) { 43 | super(target, mutable); 44 | } 45 | 46 | @Override 47 | public boolean sendKeyEvent(KeyEvent event) { 48 | if (keyListener != null) { 49 | keyListener.onKey(WiseEditText.this,event.getKeyCode(),event); 50 | } 51 | return super.sendKeyEvent(event); 52 | } 53 | 54 | @Override 55 | public boolean deleteSurroundingText(int beforeLength, int afterLength) { 56 | // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace 57 | if (beforeLength == 1 && afterLength == 0) { 58 | // backspace 59 | return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) 60 | && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); 61 | } 62 | 63 | return super.deleteSurroundingText(beforeLength, afterLength); 64 | } 65 | 66 | } 67 | 68 | //设置监听回调 69 | public void setSoftKeyListener(OnKeyListener listener){ 70 | keyListener = listener; 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_text_focused.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_text_normal.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/cursor.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 26 |