0

如果蓝牙关闭,我需要更改 Imageview 的背景图像

我有一个 Imageview 并单击它,如果打开了蓝牙,我正在设置一个图像,然后第二次单击我应该关闭蓝牙并且应该更改 imageview 的背景

我尝试了很多,但它在第二次点击时没有改变

这是我的代码

            bluetoothimg.setOnClickListener(new View. OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(!mBtAdapter.isEnabled()){
            final Intent intent = new Intent(Intent.ACTION_MAIN, null);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            int REQUEST_ENABLE_BT = 1;
            final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.bluetooth.BluetoothSettings");  
            intent.setComponent(cn);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            bluetoothimg.setImageResource(R.drawable.bt);
            startActivityForResult( intent,  REQUEST_ENABLE_BT);
            //BluetoothAdapter.getDefaultAdapter().enable();
            mBtAdapter.enable();

            }
            else {

                bluetoothimg.setImageResource(R.drawable.bt_grey);
                //BluetoothAdapter.getDefaultAdapter();
                mBtAdapter.disable();


            }
4

1 回答 1

0

以下片段将起作用

bluetoothimg.setOnClickListener(new View. OnClickListener() {

        public void onClick(View v) {

      if(bluetoothimg.getTag().toString().equalsIgnoreCase("off")) //Bluetooth Disabled
      {

        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

      }
      else   //Disable Bluetooth
      {

                if(mBtAdapter.disable())
                {
                  bluetoothImg.setTag("off");
                  bluetoothimg.setImageResource(R.drawable.bt_gray); 
                } 

       }


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(resultCode=RESULT_OK)
                {
                   bluetoothImg.setTag("on");
                   bluetoothimg.setImageResource(R.drawable.bt); 
                }
        super.onActivityResult(requestCode, resultCode, data);
    }
于 2012-06-01T05:30:21.303 回答