我想要的内容:我想创建一个编辑文本,如果给定的输入为空或无效,该文本将发生振动。
示例::在登录屏幕上,密码编辑文本为空或无效,然后我的android设备需要振动一段时间才能创建该密码?
/>
预先感谢
答案 0 :(得分:0)
好的,这就是您所需要的。
将这两个xml文件放入res/anim
文件夹
vibrate.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="10"
android:duration="1000"
android:interpolator="@anim/cycle_5" />
cycle_5.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="5" />
使用这些方法使手机振动500毫秒
public void shakeItBaby() {
int DURATION = 500; // you can change this according to your need
if (Build.VERSION.SDK_INT >= 26) {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(DURATION, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(DURATION);
}
}
别忘了把许可放进清单
<uses-permission android:name="android.permission.VIBRATE" />
现在同时使用它们验证您的EditText
Animation vibrate = AnimationUtils.loadAnimation(this, R.anim.vibrate);
if (paytm_amt.getText().toString().trim().isEmpty()) {
paytm_num.setError("Please Enter PayTM Number");
paytm_num.startAnimation(vibrate);
shakeItBaby();
} else {
// do something
}
快乐编码。
答案 1 :(得分:0)
组件
EditText mPassword = findViewById(R.id.edtPassword);
Button mLogin = findViewById(R.id.btnLogin);
Android设备振动
private void AndroidDeviceVibrate() { // Android Device Vibration
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 500 milliseconds only
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
v.vibrate(500); // deprecated in API 26
}
}
编辑文字振动动画
public TranslateAnimation VibrateError() { // Edit text vibrate Animation
TranslateAnimation vibrate = new TranslateAnimation(0, 10, 0, 0);
vibrate.setDuration(600);
vibrate.setInterpolator(new CycleInterpolator(8));
return vibrate;
}
最后,
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String strPass = mPassword.getText().toString();
if (TextUtils.isEmpty(strPass)) {
AndroidDeviceVibrate(); // Android Device Vibrate
mPassword.startAnimation(VibrateError()); // Edit text vibrate Animation
}
}
});