如何在按钮列表中选择一个按钮?

我在 LinearLayout 中有多个按钮,它们都有不同的点击选项。我想当我选择其中一个时,它会被选中并更改其背景,然后我选择另一个第一个选中的消失,然后选择新的一个回合,背景会改变。我有 8-9 个按钮或更多。我怎样才能做到这一点?


这是 action_main.xml


  <HorizontalScrollView

        android:id="@+id/horScrollView"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:scrollbars="horizontal">

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal"

            android:padding="5dp">


            <ImageButton

                android:id="@+id/spaceship"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                app:srcCompat="@drawable/spaceship" />

            <ImageButton

                android:id="@+id/mask"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginStart="10dp"

                app:srcCompat="@drawable/mask" />

        </LinearLayout>

  </HorizontalScrollView>

这是 Main.java


 ImageButton spaces = findViewById(R.id.spaceship);

        spaces.setBackgroundColor(Color.TRANSPARENT);

        spaces.setOnClickListener(view -> {


            if(getChecked())

                spaces.setBackgroundColor(Color.TRANSPARENT);

            else

                spaces.setBackgroundColor(Color.DKGRAY);


            setChecked(!check);


            modelLoader.loadModel(this, R.raw.sample_ship);

            setRenderable(spaceShipRendeble);


首先,我设置检查变量“false”。


例如,一个按钮被选中,它变成灰色,而不是我选择另一个按钮变成灰色,第一次选择将变成透明。但我做不到。


青春有我
浏览 184回答 3
3回答

牛魔王的故事

@OnClick({R.id.next,R.id.country_us,R.id.country_india})public void nextClicked(View view){ switch(view.getId()) {&nbsp; &nbsp; case R.id.next:&nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(this, TutorialScreen.class);&nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra("country_selected", ind_sel);&nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case R.id.country_india:&nbsp; &nbsp; &nbsp; &nbsp; ind_sel=true;&nbsp; &nbsp; &nbsp; &nbsp; setselectedBlank();&nbsp; &nbsp; &nbsp; &nbsp; view.setBackgroundResource(R.drawable.selected_border);&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case R.id.country_us:&nbsp; &nbsp; &nbsp; &nbsp; ind_sel=false;&nbsp; &nbsp; &nbsp; &nbsp; setselectedBlank();&nbsp; &nbsp; &nbsp; &nbsp; view.setBackgroundResource(R.drawable.selected_border);&nbsp; &nbsp; &nbsp; &nbsp; break;}}private void setselectedBlank() {&nbsp; &nbsp; country_us.setBackgroundResource(R.drawable.border);&nbsp; &nbsp; country_india.setBackgroundResource(R.drawable.border);}你可以试试这个,只需调用一个方法先将所有背景更改为默认值,然后再将其更改为选定颜色请注意我在这里使用 ButterKnife 注入库,但即使你不使用它也不是问题快乐编码&nbsp; &nbsp; &nbsp;ImageButton spaces = findViewById(R.id.spaceship);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spaces.setBackgroundColor(Color.TRANSPARENT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spaces.setOnClickListener(view -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setAllnotSelected();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spaces.setBackgroundColor(Color.DKGRAY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp;ImageButton mask = findViewById(R.id.mask);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mask.setBackgroundColor(Color.TRANSPARENT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mask.setOnClickListener(view -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setAllnotSelected();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mask.setBackgroundColor(Color.DKGRAY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });public void setAllnotSelected();{mask.setBackgroundColor(Color.TRANSPARENT);spaces.setBackgroundColor(Color.TRANSPARENT);}

撒科打诨

您可以检查一个ImageButton是否已检查的条件。基于此,您可以使所有其他setChecked()错误。我使用相同的设置值RadioButtons。if (Friends != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Friends.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isChecked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; friend_source = "Friend";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Newspaper.setChecked(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Facebook.setChecked(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Zomato.setChecked(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RadioSource.setChecked(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

哈士奇WWW

为按钮的不同状态创建一个具有不同颜色的资源文件。btn_bg_states.xml:<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_focused="true">&nbsp; &nbsp; <shape>&nbsp; &nbsp; &nbsp; &nbsp; <solid android:color="your_color" />&nbsp; &nbsp; </shape></item><item android:state_pressed="true">&nbsp; &nbsp; <shape>&nbsp; &nbsp; &nbsp; &nbsp; <solid android:color="your_other_color" />&nbsp; &nbsp; </shape></item><item>&nbsp; &nbsp; <shape>&nbsp; &nbsp; &nbsp; &nbsp; <solid android:color="your_default_color" />&nbsp; &nbsp; </shape></item></selector>然后将其设置为所有按钮的可绘制背景,Android 将为您处理其余部分。android:background="@drawable/btn_bg_states"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java