hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
Switch = (ImageButton) findViewById(R.id.Switch_off);
Switch.setOnClickListener(new OnClickListener() {
public void onClick( View vw) {
if (vw.getId() == R.id.Switch_off && !hasFlash) {
Log.d("tagname","string you want to execute");
Toast.makeText(getApplicationContext(),
"Sorry,your device doesn't support flashlight",
Toast.LENGTH_LONG).show();
}
}
});
我正在寻求按下开关,如果我没有闪光灯,我的吐司就出现了。 虽然现在当我运行此代码时,即使我没有闪光灯也按下按钮,它会正常切换而不会打开任何闪光灯。 有些设备可能没有闪存,这就是我想要的原因,当他们按下按钮给他们留下带有吐司的消息时
先谢谢!
答案 0 :(得分:0)
尝试修改此代码,现在当设备没有闪光时,您的按钮将被禁用。
hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
Switch = (ImageButton) findViewById(R.id.Switch_off);
if (!hasFlash) {
Log.d("tagname","string you want to execute");
Toast.makeText(getApplicationContext(),
"Sorry,your device doesn't support flashlight",
Toast.LENGTH_LONG).show();
switch.setEnabled(false);
}
Switch.setOnClickListener(new OnClickListener() {
public void onClick( View vw) {
//Do your work here
}
});