Android 振动点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20149415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vibrate onclick
提问by Rui Miranda
Is there a way to get a button to vibrate but only when the if condition is verified?
有没有办法让按钮振动,但只有在 if 条件得到验证时?
Here's the code:
这是代码:
Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE) ;
if(l2>=l1){
insertactone.setBackgroundColor(Color.RED);
};
here is the onclick method for insertactone:
这是 insertactone 的 onclick 方法:
einsertactone = (Button) findViewById(R.id.bsqlinsertactone);
insertactone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bsqlinsertactone:
insertactoneClick();
break;
}
}
private void insertactoneClick() {
startActivity(new Intent(
"com.example.everydaybudgetplanner.ACTONESQLENTRY"));
}
});
I want it to vibrate only if the the IF condition is verified.
我希望它仅在 IF 条件得到验证时振动。
回答by Bryan Herbst
is there a way to get a button to vibrate but only when the if condition is verified?
有没有办法让按钮振动,但只有在 if 条件得到验证时?
Yes. It looks like you have 95% of the code there already. Where did you get stuck?
是的。看起来您已经拥有 95% 的代码。你在哪里卡住了?
You already have a Vibrator
Object and a conditional. All you need to do now is call vibrate()
like so:
你已经有了一个Vibrator
对象和一个条件。你现在需要做的就是vibrate()
像这样调用:
Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if(l2 >= l1) {
insertactone.setBackgroundColor(Color.RED);
vibe.vibrate(100);
}
Don't forget that you need to add
不要忘记你需要添加
<uses-permission android:name="android.permission.VIBRATE" />
in your AndroidManifest.xml file.
在您的 AndroidManifest.xml 文件中。
回答by Seymur Mammadli
<uses-permission android:name="android.permission.VIBRATE" />
Be sure you added permission at AndroidManifest
<uses-permission android:name="android.permission.VIBRATE" />
确保您在 AndroidManifest 中添加了权限