首先,我是一个非常初学Java和编程的人。我试着让设备在某个文本框显示或点击选择按钮时振动。然而,即使我尝试了其他用户的几个建议,我也无法弄清楚如何振动。
如果你能一步一步地解释,我将不胜感激。先谢谢你!!
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.os.Vibrator;
import java.util.Arrays;
public class Story {
Hospital1 h1;
String c1, c2, c3, c4, t;
public Story(Hospital1 h1) {
this.h1 = h1;
}
public void selectNext(String next) {
switch (next) {
// hospital 1
case "h11": hospital11(); break;
case "h12": hospital12(); break;
case "h13": hospital13(); break;
case "h14": hospital14(); break;
case "h15": hospital15(); break;
case "h16": hospital16(); break;
case "h17": hospital17(); break;
case "h18": hospital18(); break;
case "h19": hospital19(); break;
case "h1s1": hospital1select1(); break;
case "h110a": hospital110a(); break;
case "h110b": hospital110b(); break;
case "h1s2": hospital1select2(); break;
case "h111a": hospital111a(); break;
case "h111b": hospital111b(); break;
case "h112": hospital112(); break;
case "h113": hospital113(); break;
.
.
.
}
public void showButton() {
for (Button button : Arrays.asList(h1.choice1, h1.choice2, h1.choice3, h1.choice4)) {
button.setVisibility(View.VISIBLE);
}
}
public void noButton() {
for (Button button : Arrays.asList(h1.choice1, h1.choice2, h1.choice3, h1.choice4)) {
button.setVisibility(View.INVISIBLE);
}
}
public void start() {
h1.background.setImageResource(R.mipmap.hospitalbed);
h1.character.setImageResource(R.mipmap.god);
h1.character.setVisibility(View.INVISIBLE);
h1.text.setText("");
noButton();
t = "h11";
}
public void hospital11() {
h1.character.setVisibility(View.VISIBLE);
h1.character.setImageResource(R.mipmap.nurse2);
h1.text.setText("");
t = "h12";
}
public void hospital12() {
h1.character.setVisibility(View.INVISIBLE);
h1.text.setText("");
t = "h13";
}
public void hospital18() {
h1.character.setVisibility(View.INVISIBLE);
h1.text.setText("");
t = "h14";
}
public void hospital14() {
h1.character.setVisibility(View.VISIBLE);
h1.text.setText("");
t = "h15";
Vibrator vibrator = (Vibrator) h1.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
}
public void hospital15() {
h1.character.setVisibility(View.VISIBLE);
h1.text.setText("");
t = "h1s1";
}
public void hospital1select1() {
h1.character.setVisibility(View.INVISIBLE);
h1.text.setVisibility(View.INVISIBLE);
showButton();
h1.choice1.setVisibility(View.INVISIBLE);
h1.choice4.setVisibility(View.INVISIBLE);
h1.choice2.setText("");
h1.choice3.setText("");
c2 = "h16a";
c3 = "h16b";;
}
1条答案
按热度按时间4ioopgfo1#
假设您的
h1
是Context
的有效Object,则h1.getSystemService(Context.VIBRATOR_SERVICE)
没有任何编译错误。您的设备应该能够通过以下代码振动:
但请注意,以上是已弃用。实现相同目的的更新方法如下:
由于Vibration需要Android中的权限,您需要在AndroidManifest.xml中添加权限
android.permission.VIBRATE
: