我想根据存储在变量中的值来触发imagebutton。
例如:将变量设为数量。如果是amount<10和amount>50,则应触发图像按钮。
在这里,我通过imagebutton打开和关闭手电筒。因此,如果
amount>10和<30,然后手电筒打开
amount>30和<50,然后手电筒关闭
其次,我从一个函数中获取字符串形式的值,该值将被转换为整数并存储在数量变量中。
Java代码:
Integer amount;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d("bluetooth_torch", "onCreate()");
setContentView(R.layout.activity_bluetooth_torch);
mTorchOnOffButton = (ImageButton)findViewById(R.id.button_on_off);
isTorchOn = false;
Boolean isFlashAvailable = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!isFlashAvailable) {
AlertDialog alert = new AlertDialog.Builder(bluetooth_torch_Activity.this)
.create();
alert.setTitle("Error !!");
alert.setMessage("Your device doesn't support flash light!");
alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
System.exit(0);
}
});
alert.show();
return;
}
mCameraManager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
try {
mCameraId = mCameraManager.getCameraIdList()[0];
} catch (CameraAccessException e) {
e.printStackTrace();
}
amount = Integer.parseInt(DATA);
mTorchOnOffButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (isTorchOn) {
turnOffFlashLight();
isTorchOn = false;
} else {
turnOnFlashLight();
isTorchOn = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}发布于 2016-11-13 19:50:58
mTorchOnOffButton.callOnClick()
发布于 2016-11-13 20:23:31
您可以通过两种方式调用按钮上的click事件:
mTorchOnOffButton.performClick();这将调用click事件,就像您自己单击按钮一样。
mTorchOnOffButton.callOnClick();这将只调用button的OnClickListener方法,并且与performClick()不同,它不会报告任何可访问性事件。
https://stackoverflow.com/questions/40573445
复制相似问题