如何使单个按钮在android中显示为活动状态

时间:2017-07-07 21:49:10

标签: java android

我已经学习了一段时间的java并正在开发一个Android应用程序。我试图为嵌套在线性布局中的图像按钮实现布尔逻辑,其中只有一个按钮应显示活动按钮而其他按钮不显示。 现在,每个按钮都可以在活动和非活动之间切换,但我不希望两个或多个按钮同时显示活动状态。我想要一个单独显示一个活动按钮。

 public void showAudio(View view) {
        if (!audioButtonClicked) {
            audioButtonClicked = true;
            audioWindowButton.setImageResource(R.drawable.audio_active);
        } else {
            audioWindowButton.setImageResource(R.drawable.audio_inactive);
            audioButtonClicked = false;
        }

        }

    public void showEvents(View view) {
       if(!eventButtonClicked ) {
eventWindowButton.setImageResource(R.drawable.event_active);
           eventButtonClicked = true;
       } else {
           eventWindowButton.setImageResource(R.drawable.event_inactive);
           eventButtonClicked = false;
       }
    }

    public void showMore(View view) {
        if(!moreButtonClicked) {
            moreWindowButton.setImageResource(R.drawable.event_active);
            moreButtonClicked = true;

        } else {
            moreWindowButton.setImageResource(R.drawable.event_inactive);
            moreButtonClicked = false;
        }
    }

3 个答案:

答案 0 :(得分:1)

您可以使用button.setVisibility()更改其可见性。这是伪代码。

if() { // some condition when you want imageButton1 to be visibile and others invisible
    imageButton1.setVisibility(View.VISIBLE);     
    imageButton2.setVisibility(View.INVISIBLE);
    imageButton3.setVisibility(View.INVISIBLE);
} else if() { // some other condition when you want imageButton2 to be visibile and others invisible
    imageButton1.setVisibility(View.INVISIBLE);
    imageButton2.setVisibility(View.VISIBLE);
    imageButton3.setVisibility(View.INVISIBLE);
} else { // when you want imageButton3 to be visibile and others invisible
    imageButton1.setVisibility(View.INVISIBLE);
    imageButton2.setVisibility(View.INVISIBLE);
    imageButton3.setVisibility(View.VISIBLE);
}

使用上述逻辑:

public void showAudio(View view) {
        if (!audioButtonClicked) {
            audioButtonClicked = true;
            eventButtonClicked = false;
            moreButtonClicked = false;
            audioWindowButton.setImageResource(R.drawable.audio_active);
            eventWindowButton.setImageResource(R.drawable.event_inactive);
            moreWindowButton.setImageResource(R.drawable.event_inactive);
        } else {
            audioWindowButton.setImageResource(R.drawable.audio_inactive);
            audioButtonClicked = false;
        }
}

public void showEvents(View view) {
        if(!eventButtonClicked ) {
            audioButtonClicked = false;
            eventButtonClicked = true;
            moreButtonClicked = false;
            audioWindowButton.setImageResource(R.drawable.audio_inactive);
            eventWindowButton.setImageResource(R.drawable.event_active);
            moreWindowButton.setImageResource(R.drawable.event_inactive);
        } else {
           eventWindowButton.setImageResource(R.drawable.event_inactive);
           eventButtonClicked = false;
        }
}

public void showMore(View view) {
        if(!moreButtonClicked) {
            audioButtonClicked = false;
            eventButtonClicked = false;
            moreButtonClicked = true;
            audioWindowButton.setImageResource(R.drawable.audio_inactive);
            eventWindowButton.setImageResource(R.drawable.event_inactive);
            moreWindowButton.setImageResource(R.drawable.event_active);

        } else {
            moreWindowButton.setImageResource(R.drawable.event_inactive);
            moreButtonClicked = false;
        }
}

仅当启用了功能而其他功能已禁用时,此功能才会显示。如果启用了任何一个功能,则其他两个功能将被禁用,但如果仅禁用一个功能,则不会更改其他功能。

答案 1 :(得分:0)

您可以使用ViewSwitcher。将物品放入其中并在它们之间切换。只有一个会出现。以下是前。有2个观点。

在layout_main.xml

<ViewSwitcher
        android:id="@+id/viewSwitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/text1"
            android:text="previous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>

        <TextView
            android:id="@+id/text2"
            android:text="next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>

</ViewSwitcher>

在MainActivity.java中

viewSwitcher =   (ViewSwitcher)findViewById(R.id.viewSwitcher);
        text1= (TextView)findViewById(R.id.text1);
        text2 = (TextView) findViewById(R.id.text2);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (viewSwitcher.getCurrentView() != text1){
                       viewSwitcher.showPrevious(); 
                } else if (viewSwitcher.getCurrentView() != text2){
                    viewSwitcher.showNext();
                }
            }
        });

答案 2 :(得分:0)

用以下代码替换您的代码:

public void showAudio(View view) {

    audioWindowButton.setImageResource(R.drawable.audio_active);
    eventWindowButton.setImageResource(R.drawable.event_inactive);
    moreWindowButton.setImageResource(R.drawable.event_inactive);

}

public void showEvents(View view) {

    audioWindowButton.setImageResource(R.drawable.audio_inactive);
    eventWindowButton.setImageResource(R.drawable.event_active);
    moreWindowButton.setImageResource(R.drawable.event_inactive);

}

public void showMore(View view) {


    audioWindowButton.setImageResource(R.drawable.audio_inactive);
    eventWindowButton.setImageResource(R.drawable.event_inactive);
    moreWindowButton.setImageResource(R.drawable.event_active);

}