我想創建一個編輯文本,如果給定的輸入無效將會振動。 例如編輯文本的數字,如果數字是錯誤的,像它包含9位數字比編輯文本將變得清晰,並會振動一段時間 如何創建? 在此先感謝振動Edittext在Android中
16
A
回答
30
創建資源動畫文件夾,然後創建文件nmaed shake.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_7" />
和其他文件cycle_7.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="schemas.android.com/apk/res/android" android:cycles="7" />
,然後在你的java文件
if(invalid)//check your input
{
Animation shake = AnimationUtils.loadAnimation(Login.this, R.anim.shake);
editText.startAnimation(shake);
}
4
振動請使用以下代碼。
Vibrator vibe = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
然後,在OnTextChanged Listener方法中使用下面的代碼。
vibe.vibrate(50); // 50 is time in ms
而且不要忘記,你需要允許添加到清單(在</application>
標籤後):
<uses-permission android:name="android.permission.VIBRATE" />
0
你應該這個監聽器添加到EditText上您想要的驗證,
editText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Write your logic here
if(condition satisfy)
// call vibrate();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
public void vibrate()
{
Vibrator vibrate= (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE) ;
vibrate.vibrate(50);
}
8
如果有人正在尋找做@Priya建議的方法以編程方式,那麼你可以試試這個。
public TranslateAnimation shakeError() {
TranslateAnimation shake = new TranslateAnimation(0, 10, 0, 0);
shake.setDuration(500);
shake.setInterpolator(new CycleInterpolator(7));
return shake;
}
然後:
myEditText.startAnimation(shakeError());
+2
與XML相比,這感覺更清晰。 – kanudo 2017-08-19 08:16:51
0
創建shake.xml
動畫文件夾
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-10"
android:toXDelta="10"
android:repeatCount="5"
android:repeatMode="reverse"
android:interpolator="@android:anim/linear_interpolator"
android:duration="70" />
</set>
下,在此之後添加動畫按鈕。爲了簡單起見,我在Kotlin中編寫了這些代碼。
button.setOnClickListener {
button.startAnimation(AnimationUtils.loadAnimation(context, R.anim.shake)
}
相關問題
- 1. Android振動API
- 2. Android振動器
- 3. Android:振動
- 4. Android振動崩潰
- 5. 在Android中滾動EditText
- 6. 振動/蜂鳴前活動中的Android
- 7. 在android中自動滾動EditText內容
- 8. Android Soundpool VS振動器
- 9. 使用片段振動android
- 10. Android的振動僅15秒
- 11. Android通知振動時間
- 12. Android手機不振動
- 13. Android上的振動器
- 14. Android:設置振動功率
- 15. Android鎖定/睡眠振動
- 16. android獲取振動設置
- 17. Android:NullpointerException在振動
- 18. Android:振動器方法(如果手機沒有振動器?)
- 19. Android中的振動監聽器
- 20. 振動無法在Android 4.0上工作
- 21. Android,睡在振動方法之間
- 22. 在滾動視圖中的EditText在android
- 23. Android振動烤麪包(荷馬:嗯嗯在烤麪包上振動)
- 24. Android:在edittext中的動畫文本
- 25. 在TableRow中MultiLine時移動Android EditText
- 26. 如何在Android中手動旋轉EditText
- 27. 在Android中動態創建EditText
- 28. 在Android中動態添加EditText
- 29. 如何在Android中向下滾動EditText
- 30. 如何在android中自動建議edittext
Vibrator vibe =(振動器)context.getSystemService(Context.VIBRATOR_SERVICE);我用這個BT它不工作 – 2013-03-14 05:37:47