Android简易实战教程--第十九话《手把手教您监听EditText文本变化,实现抖动和震动的效果》

时间:2021-03-06 01:54:47

昨晚写博客太仓促,代码结构有问题,早上测试发现没法监听文本变化!今日更改一下。真心见谅啦,哈哈!主活动的代码已经改好了,看截图这次的确实现了文本监听变化情况。

监听文本输入情况,仅仅限于土司略显low点,这一篇就稍微“高大上”些,体验一下滚动和震动。

首先,需要两个文件。截图:

Android简易实战教程--第十九话《手把手教您监听EditText文本变化,实现抖动和震动的效果》

两个文件的内容分别如下:

cycle_7:

<?xml version="1.0" encoding="utf-8"?>
<!-- 表示循环的次数 -->
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="7" />

shake.xml:

<?xml version="1.0" encoding="utf-8"?>

<!-- 位移动画 。时间1秒,位移x(0,10)。抖动循环次数7次。interpolator是动画插入器-->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:interpolator="@anim/cycle_7"
android:toXDelta="10" />

接下来进入主题了。先来个界面:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" /> <Button
android:id="@+id/bt_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询" /> <TextView
android:id="@+id/tv_showresult"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout>

需求:在编辑框输入密码,判断是否正确。如果输入正确,就在下边显示正确(这里做监听文本变化);如果错误,手机震动(震动效果需要权限、真机测试);如果输入为空,编辑框实现左右滚动。

代码写了出来:

package com.itydl.shake;

import android.app.Activity;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends Activity { private EditText et_input;
private TextView tv_result;
private Button bt_query; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); et_input = (EditText) findViewById(R.id.et_input); tv_result = (TextView) findViewById(R.id.tv_showresult); bt_query = (Button) findViewById(R.id.bt_query); initEvent();
} private void initEvent() {
// 监听文本变化.EditText文本发生变的时候调用下面相应的方法
et_input.addTextChangedListener(new TextWatcher() { @Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// 文本变化的时候调用 } @Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// 文本变化之前调用 } @Override
public void afterTextChanged(Editable s) {
// 文本变化之后调用
showResult2(); }
}); bt_query.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
showResult2(); } }); } private void showResult2() {
// 获取编辑框内容
String inputpass = et_input.getText().toString().trim();
// 判断输入是否为空,为空就实现滚动效果
if (TextUtils.isEmpty(inputpass)) {
// 为空,滚动
Animation shake = AnimationUtils.loadAnimation(MainActivity.this,
R.anim.shake);// 需要R.anim.shake资源文件
et_input.startAnimation(shake);
tv_result.setText("");
return;
} else { // 判断密码是否是123
if (inputpass.equals("123")) {
tv_result.setText("恭喜你,正确!");
} else {
// 不正确,让手机震动
/*
* 震动效果
*/
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
// 震动的参数设置 (两个参数:1、震动、休息、震动、休息....交替循环;2、重复次数为3次)
vibrator.vibrate(new long[] { 200, 300, 300, 200, 500, 100 }, 3);
tv_result.setText("");
}
}
} }

震动效果记得加上震动的权限:(震动真机才能测试哦)

<uses-permission android:name="android.permission.VIBRATE"/>

好了,运行程序看看效果:

Android简易实战教程--第十九话《手把手教您监听EditText文本变化,实现抖动和震动的效果》

我们发现密码正确或者错误的时候,下边的textview自动就显示正确错误了。

怎么样?比起土司,是不是略显“高大上”呢?赶快玩起来吧!

欢迎大家关注我的博客:点击打开链接      点击打开链接   http://blog.csdn.net/qq_32059827      观看更多更好玩的案例,每日一更哦!