0

Bluetoothがオフの場合、Imageviewの背景画像を変更する必要があります

私はImageviewを持っていて、Bluetoothがオンになっている場合は、1つの画像を設定し、2回目のクリックで、Bluetoothをオフにして、ImageViewの背景を変更する必要があります。

たくさん試しましたが、2回目のクリックでも変わりません

これが私のコードです

            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 に答える