为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

青春有我 2022-06-30 19:12:35
我在 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”。例如,一个按钮被选中,它变成灰色,而不是我选择另一个按钮变成灰色,第一次选择将变成透明。但我做不到。
查看完整描述

3 回答

?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

@OnClick({R.id.next,R.id.country_us,R.id.country_india})

public void nextClicked(View view)

{ switch(view.getId()) {

    case R.id.next:

        Intent intent = new Intent(this, TutorialScreen.class);

        intent.putExtra("country_selected", ind_sel);

        startActivity(intent);

        break;

    case R.id.country_india:

        ind_sel=true;

        setselectedBlank();

        view.setBackgroundResource(R.drawable.selected_border);

        break;


    case R.id.country_us:

        ind_sel=false;

        setselectedBlank();

        view.setBackgroundResource(R.drawable.selected_border);

        break;

}

}



private void setselectedBlank() {

    country_us.setBackgroundResource(R.drawable.border);

    country_india.setBackgroundResource(R.drawable.border);

}

你可以试试这个,只需调用一个方法先将所有背景更改为默认值,然后再将其更改为选定颜色


请注意我在这里使用 ButterKnife 注入库,但即使你不使用它也不是问题快乐编码


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

            spaces.setBackgroundColor(Color.TRANSPARENT);

            spaces.setOnClickListener(view -> {



              setAllnotSelected();

                    spaces.setBackgroundColor(Color.DKGRAY);



            });

     ImageButton mask = findViewById(R.id.mask);

            mask.setBackgroundColor(Color.TRANSPARENT);

            mask.setOnClickListener(view -> {


               setAllnotSelected();

                    mask.setBackgroundColor(Color.DKGRAY);



            });


public void setAllnotSelected();

{

mask.setBackgroundColor(Color.TRANSPARENT);

spaces.setBackgroundColor(Color.TRANSPARENT);

}


查看完整回答
反对 回复 2022-06-30
?
撒科打诨

TA贡献1934条经验 获得超2个赞

您可以检查一个ImageButton是否已检查的条件。基于此,您可以使所有其他setChecked()错误。我使用相同的设置值RadioButtons。


if (Friends != null) {

                        Friends.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                            @Override

                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                                if (isChecked) {

                                    friend_source = "Friend";

                                    Newspaper.setChecked(false);

                                    Facebook.setChecked(false);

                                    Zomato.setChecked(false);

                                    RadioSource.setChecked(false);

                                }



                            }

                        });

                    }


查看完整回答
反对 回复 2022-06-30
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

为按钮的不同状态创建一个具有不同颜色的资源文件。


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">

    <shape>

        <solid android:color="your_color" />

    </shape>

</item>


<item android:state_pressed="true">

    <shape>

        <solid android:color="your_other_color" />

    </shape>

</item>



<item>

    <shape>

        <solid android:color="your_default_color" />


    </shape>

</item>

</selector>

然后将其设置为所有按钮的可绘制背景,Android 将为您处理其余部分。


android:background="@drawable/btn_bg_states"


查看完整回答
反对 回复 2022-06-30
  • 3 回答
  • 0 关注
  • 183 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号