按钮点击效果只在android中的一个按钮

时间:2013-10-16 13:00:48

标签: android android-imageview onclicklistener android-selector

我做了一个活动,我已经将4个imageViews作为按钮,并应用了不同的4个选择器,现在我需要的是当我按下按下状态的1个按钮时,其他3个按钮无法按下。我的选择器和代码如下: 的 selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/connect"  android:state_pressed="true"/>
    <item android:drawable="@drawable/disconnected"/>

</selector>

class.java

public class GetInActionActivity extends Activity implements OnClickListener {
    LinearLayout ll_gec_intro;
    private ImageView connected;
    private ImageView disConnected;
    private ImageView reConnected;
    private ImageView unConnected;
    public ImageButton imageButton_home;
    public Boolean isClicked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grace_in_action);
        ll_gec_intro = (LinearLayout) findViewById(R.id.ll_day_intro);
        this.connected = (ImageView) findViewById(R.id.connected);
        this.disConnected = (ImageView) findViewById(R.id.disconnected);
        this.reConnected = (ImageView) findViewById(R.id.reconnected);
        this.unConnected = (ImageView) findViewById(R.id.unconnected);
        imageButton_home = (ImageButton) findViewById(R.id.imageButton_home);
        imageButton_home.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GetInActionActivity.this,
                        HomeActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
            }
        });
        ll_gec_intro.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(GetInActionActivity.this,
                        GraceInActionIntroductionActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);

            }
        });
        connected.setOnClickListener(this);
        disConnected.setOnClickListener(this);
        unConnected.setOnClickListener(this);
        reConnected.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent i = new Intent();
        switch (v.getId()) {
        case R.id.connected:
            if (isClicked) {
                return;
            } else {
                i = new Intent(GetInActionActivity.this, SocialActivity.class);
                Config.conn_type = 1;
                disConnected.setClickable(false);
                unConnected.setClickable(false);
                reConnected.setClickable(false);
                startActivity(i);
                disConnected.setBackgroundResource(R.drawable.disconnected);
                unConnected.setBackgroundResource(R.drawable.unconnected);
                reConnected.setBackgroundResource(R.drawable.reconnecting);
                isClicked = true;

                break;
            }
        case R.id.disconnected:
            if (isClicked) {
                return;
            } else {
                i = new Intent(GetInActionActivity.this, SocialActivity.class);
                Config.conn_type = 2;
                connected.setClickable(false);
                unConnected.setClickable(false);
                reConnected.setClickable(false);
                startActivity(i);
                isClicked = true;
                connected.setBackgroundResource(R.drawable.connected);
                unConnected.setBackgroundResource(R.drawable.unconnected);
                reConnected.setBackgroundResource(R.drawable.reconnecting);
                break;
            }
        case R.id.reconnected:
            if (isClicked) {
                return;
            } else {
                i = new Intent(GetInActionActivity.this, SocialActivity.class);
                Config.conn_type = 3;
                disConnected.setClickable(false);
                unConnected.setClickable(false);
                connected.setClickable(false);
                startActivity(i);
                isClicked = true;
                disConnected.setBackgroundResource(R.drawable.disconnected);
                unConnected.setBackgroundResource(R.drawable.unconnected);
                connected.setBackgroundResource(R.drawable.connected);
                break;
            }
        case R.id.unconnected:
            if (isClicked) {
                return;
            } else {
                i = new Intent(GetInActionActivity.this, SocialActivity.class);
                Config.conn_type = 4;
                disConnected.setClickable(false);
                connected.setClickable(false);
                reConnected.setClickable(false);
                startActivity(i);
                isClicked = true;
                disConnected.setBackgroundResource(R.drawable.disconnected);
                connected.setBackgroundResource(R.drawable.connected);
                reConnected.setBackgroundResource(R.drawable.reconnecting);
                break;
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        isClicked = false;
        connected.setClickable(true);
        disConnected.setClickable(true);
        unConnected.setClickable(true);
        reConnected.setClickable(true);
        unConnected.setBackgroundResource(R.drawable.unconnected);
        disConnected.setBackgroundResource(R.drawable.disconnected);
        connected.setBackgroundResource(R.drawable.connected);
        reConnected.setBackgroundResource(R.drawable.reconnecting);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        isClicked = false;
        connected.setClickable(true);
        disConnected.setClickable(true);
        unConnected.setClickable(true);
        reConnected.setClickable(true);
        unConnected.setBackgroundResource(R.drawable.unconnected);
        disConnected.setBackgroundResource(R.drawable.disconnected);
        connected.setBackgroundResource(R.drawable.connected);
        reConnected.setBackgroundResource(R.drawable.reconnecting);
    }
}

请帮我解决..

1 个答案:

答案 0 :(得分:0)

你可以这样做。它不是你的问题的完整实现。它只是一个提示。

 @Override
    public void onClick(View v) {
        Intent i = new Intent();
        switch (v.getId()) {
        case R.id.connected:
            if (isClicked) {
                return;
            } else {
                i = new Intent(GetInActionActivity.this, SocialActivity.class);
                Config.conn_type = 1;
                disConnected.setonClickListener(null);
                unConnected.setonClickListener(null);
                reConnected.setonClickListener(null);
                startActivity(i);
                disConnected.setBackgroundResource(R.drawable.disconnected);
                unConnected.setBackgroundResource(R.drawable.unconnected);
                reConnected.setBackgroundResource(R.drawable.reconnecting);
                isClicked = true;

                break;
            }
        case R.id.disconnected:
            if (isClicked) {
                return;
            } else {
                i = new Intent(GetInActionActivity.this, SocialActivity.class);
                Config.conn_type = 2;
                connected.setonClickListener(null);
                unConnected.setonClickListener(null);
                reConnected.setonClickListener(null);
                startActivity(i);
                isClicked = true;
                connected.setBackgroundResource(R.drawable.connected);
                unConnected.setBackgroundResource(R.drawable.unconnected);
                reConnected.setBackgroundResource(R.drawable.reconnecting);
                break;
            }
        case R.id.reconnected:
            if (isClicked) {
                return;
            } else {
                i = new Intent(GetInActionActivity.this, SocialActivity.class);
                Config.conn_type = 3;
                disConnected.setonClickListener(null);
                unConnected.setonClickListener(null);
                connected.setonClickListener(null);
                startActivity(i);
                isClicked = true;
                disConnected.setBackgroundResource(R.drawable.disconnected);
                unConnected.setBackgroundResource(R.drawable.unconnected);
                connected.setBackgroundResource(R.drawable.connected);
                break;
            }
        case R.id.unconnected:
            if (isClicked) {
                return;
            } else {
                i = new Intent(GetInActionActivity.this, SocialActivity.class);
                Config.conn_type = 4;
                disConnected.setonClickListener(null);
                connected.setonClickListener(null);
                reConnected.setonClickListener(null);
                startActivity(i);
                isClicked = true;
                disConnected.setBackgroundResource(R.drawable.disconnected);
                connected.setBackgroundResource(R.drawable.connected);
                reConnected.setBackgroundResource(R.drawable.reconnecting);
                break;
            }
        }