监听文本输入情况,仅仅限于土司略显low点,这一篇就稍微“高大上”些,体验一下滚动和震动。
首先,需要两个文件。截图:
两个文件的内容分别如下:
cycle_7:
<?xml version="1.0" encoding="utf-8"?>
<!-- 表示循环的次数 -->
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="7" /><?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) {
// 文本变化之后调用
showResult();
}
});
}
public void showResult(){
bt_query.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//获取编辑框内容
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;
}
//判断面膜是否是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"/>
怎么样?比起土司,是不是略显“高大上”呢?赶快玩起来吧!
欢迎大家关注我的博客:点击打开链接
点击打开链接 http://blog.csdn.net/qq_32059827 观看更多更好玩的案例,每日一更哦!
Android简易实战教程--第十九话《手把手教您监听EditText文本变化,实现抖动和震动的效果》
原文:http://blog.csdn.net/qq_32059827/article/details/52245374