如果蓝牙关闭,则更改Imageview背景

时间:2012-06-01 05:10:28

标签: android bluetooth wifi

如果蓝牙关闭,我需要更改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();


            }

1 个答案:

答案 0 :(得分: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);
    }