java - 如何在 android studio 中以编程方式触发图像按钮

标签 java android

我想根据变量中存储的值触发图像按钮。
例如:设变量为金额。然后,如果 amount<10 且 amount>50,则应触发图像按钮。
在这里,通过图像按钮我打开和关闭手电筒。所以,如果
金额>10且<30则手电筒打开
amount>30 和 <50,然后手电筒关闭

其次,我从函数中获取字符串形式的值,该值将转换为整数并存储在 amount 变量中。

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();
                }
            }
        });
    }

最佳答案

mTorchOnOffButton.callOnClick()

关于java - 如何在 android studio 中以编程方式触发图像按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40573445/

相关文章:

java - 枚举外部驱动器

android - Protractor + Appium 在 chrome 中隐藏 android 键盘

java - 在 Android 上从蓝牙数据传输读取数据

java - Activity中的静态变量会导致内存泄漏吗?

android - 如何在服务类的定时器内显示AlertDialog?

Java字符串返回方法不返回字符串?

java - 一列布局不使用整个窗口宽度

java - GRAILS:永久链接/slug 生成方法?

java - Java 中的简单*权威 DNS 服务器*

javascript - 使用 HTML5/JS 的移动设备后置摄像头