【问题标题】:Could my drawable be interfering with my button state?我的drawable会干扰我的按钮状态吗?
【发布时间】:2014-10-25 12:41:08
【问题描述】:

现在,我有一个单选组作为父级,切换按钮作为子级。

所以是这样的:

布局.xml

<RadioGroup

                android:id="@+id/toggleGroup"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:background="@color/white"
                android:useDefaultMargins="true"

                android:layout_column="0"
                android:columnCount="11"
                android:rowCount="1"
                android:orientation="horizontal"
>

        <ToggleButton

                    android:id="@+id/number_zero"
                    android:layout_width="34sp"
                    android:layout_height="40sp"
                    android:layout_row="0"
                    android:layout_column="0"
                    android:textOn="@string/number_zero"
                    android:textOff="@string/number_zero"
                    android:layout_margin="5sp"

                    />
        <ToggleButton
                    android:id="@+id/number_one"
                    android:layout_width="34sp"
                    android:layout_height="40sp"
                    android:layout_row="0"
                    android:layout_column="1"
                    android:textOn="@string/number_one"
                    android:textOff="@string/number_one"
                    android:layout_margin="5sp"

                />
</RadioGroup>
</GridLayout> 

Adapter中的代码sn-p:

ToggleButton one;

one = (ToggleButton) view.findViewById(R.id.number_one);

 one.setOnClickListener(new View.OnClickListener()  {
                    public void onClick(View view) {
                        buttonValue = 1;

                        ToggleButton tb = (ToggleButton) view;

                        if(tb.isChecked()){

                            RadioGroup radioGroup = (RadioGroup) tb.getParent();

                            for(int i=0; i<(radioGroup).getChildCount(); ++i) {
                                View nextChild = (radioGroup).getChildAt(i);
                                if(nextChild instanceof ToggleButton && nextChild.getId()==tb.getId() ){


                                }else if (nextChild instanceof ToggleButton && nextChild.getId()!=tb.getId() ){

                                    ToggleButton tb2=(ToggleButton) nextChild;
                                    tb2.setChecked(false);
                                }
                            }

                        } else{
                            RadioGroup radioGroup = (RadioGroup) tb.getParent();

                            for(int i=0; i<(radioGroup).getChildCount(); ++i) {
                                View nextChild = (radioGroup).getChildAt(i);
                                if(nextChild instanceof ToggleButton && nextChild.getId()==tb.getId() ){
                                    ToggleButton tb2=(ToggleButton) nextChild;
                                    tb2.setChecked(false);
                                }else if (nextChild instanceof ToggleButton && nextChild.getId()!=tb.getId() ){
                                    ToggleButton tb2=(ToggleButton) nextChild;
                                    tb2.setChecked(false);

                                }
                            }
                        }
                    }
                });

以上内容针对我拥有的每个按钮重复。在此示例中为按钮 0 和 1。

可绘制:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:state_pressed="true" android:state_checked="true" >
        <shape>
            <solid
                    android:color="@color/button_selected" />
            <stroke
                    android:width="1dp"
                    android:color="@color/button_selected"  />
            <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
        </shape>
    </item >
    <item>
        <shape android:state_checked="false">
            <solid
                    android:color="@color/number_button_grey" />
            <stroke
                    android:width="1dp"
                    android:color="@color/number_button_grey" />

            <padding
                    android:left="10dp"
                    android:top="10dp"
                    android:right="10dp"
                    android:bottom="10dp" />
        </shape>
    </item>
</selector>

我想要的行为:

我想在按钮之间切换,以便一次只按下一个按钮。例如,在这种情况下,如果我单击按钮零,则将按下零并且不按下按钮一。如果我单击按钮一,则按钮一被按下,而按钮零则不被按下。我希望按下状态为真。

出于某种原因,如果我删除了上面按钮的可绘制对象,我可以像单选按钮行为一样轻松切换,当我按下按钮时,它会保持按下状态。但是,当我应用我的可绘制对象时,按钮在按下时不会保持按下状态。只有当我单击按钮并用手指按住按钮时,状态才会改变。如果我移开手指,它会返回按下状态 = false。

我不确定为什么使用可绘制对象,这不是我想要的行为。

编辑:这是一个关于当我将切换按钮更改为单选按钮以及当我拥有切换按钮时的行为的视频。我在单选按钮之间切换,红色选择没有保持不变。我对切换按钮做同样的事情。 https://www.dropbox.com/s/9sinptgoucv1hvn/VIDEO0043.mp4?dl=0

【问题讨论】:

  • 为什么在你的第二个项目中,形状有 an​​droid:state_checked 属性?
  • 只是为了强制在未选择按钮时显示灰色。我应该删除它吗?
  • 每个按钮都有一个默认状态。并且该项目不需要任何属性集。我不知道它是否会起作用,如果你删除它。但这肯定无济于事。看例子here
  • 是的,改变它没有帮助。我仍然有同样的问题。有什么想法吗?
  • @DuZi,我在帖子中添加了一个短视频来查看我的应用程序的问题。

标签: java android button android-drawable


【解决方案1】:

我想你正在寻找这个:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:background="#696969"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/toggleGroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_column="0"
        android:background="#FFFFFF"
        android:columnCount="4"
        android:orientation="horizontal"
        android:rowCount="1"
        android:useDefaultMargins="true" >

        <ToggleButton
            android:id="@+id/number_one"
            android:layout_width="34sp"
            android:layout_height="40sp"
            android:layout_margin="5sp"
            android:background="@drawable/button_style"
            android:textOff="1"
            android:textOn="1" />

        <ToggleButton
            android:id="@+id/number_two"
            android:layout_width="34sp"
            android:layout_height="40sp"
            android:layout_margin="5sp"
            android:background="@drawable/button_style"
            android:textOff="2"
            android:textOn="2" />

        <ToggleButton
            android:id="@+id/number_three"
            android:layout_width="34sp"
            android:layout_height="40sp"
            android:layout_margin="5sp"
            android:background="@drawable/button_style"
            android:textOff="3"
            android:textOn="3" />

        <ToggleButton
            android:id="@+id/number_four"
            android:layout_width="34sp"
            android:layout_height="40sp"
            android:layout_margin="5sp"
            android:background="@drawable/button_style"
            android:textOff="4"
            android:textOn="4" />

        <ToggleButton
            android:id="@+id/number_five"
            android:layout_width="34sp"
            android:layout_height="40sp"
            android:layout_margin="5sp"
            android:background="@drawable/button_style"
            android:textOff="5"
            android:textOn="5" />
    </RadioGroup>

</LinearLayout>

button_style.xml

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

    <item android:state_pressed="true" >
        <shape>
            <gradient
               android:endColor="#99BCA259"
               android:startColor="#99BCA259"
               android:angle="270" />
            <stroke
               android:width="0dp"
               android:color="#99515A91" />
            <corners
               android:radius="0dp" />
            <padding
               android:left="0dp"
               android:top="0dp"
               android:right="0dp"
               android:bottom="0dp" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
               android:endColor="#99BCA259"
               android:startColor="#99BCA259"
               android:angle="270" />
            <stroke
               android:width="0dp"
               android:color="#99BCA259" />
            <corners
               android:radius="0dp" />
            <padding
               android:left="0dp"
               android:top="0dp"
               android:right="0dp"
               android:bottom="0dp" />
        </shape>
    </item>

    <item android:state_checked="true" >
        <shape>
            <gradient
               android:endColor="#99BCA259"
               android:startColor="#99BCA259"
               android:angle="270" />
            <stroke
               android:width="0dp"
               android:color="#99BCA259" />
            <corners
               android:radius="0dp" />
            <padding
               android:left="0dp"
               android:top="0dp"
               android:right="0dp"
               android:bottom="0dp" />
        </shape>
    </item>

    <item android:state_enabled="false">
        <shape>
          <gradient
             android:endColor="#99454545"
             android:startColor="#99454545"
             android:angle="270" />
          <stroke
             android:width="0dp"
             android:color="#99454545" />
          <corners
             android:radius="0dp" />
          <padding
             android:left="0dp"
             android:top="0dp"
             android:right="0dp"
             android:bottom="0dp" />
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
               android:endColor="#99454545"
               android:startColor="#99454545"
               android:angle="270" />
            <stroke
               android:width="0dp"
               android:color="#99454545" />
            <corners
               android:radius="0dp" />
            <padding
               android:left="0dp"
               android:top="0dp"
               android:right="0dp"
               android:bottom="0dp" />
        </shape>
    </item>
</selector>

MainAcitivity.java

public class MainActivity extends Activity  implements OnClickListener{
    ToggleButton tb1, tb2, tb3, tb4, tb5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tb1 = (ToggleButton) findViewById(R.id.number_one);
        tb2 = (ToggleButton) findViewById(R.id.number_two);
        tb3 = (ToggleButton) findViewById(R.id.number_three);
        tb4 = (ToggleButton) findViewById(R.id.number_four);
        tb5 = (ToggleButton) findViewById(R.id.number_five);

        tb1.setOnClickListener(this);
        tb2.setOnClickListener(this);
        tb3.setOnClickListener(this);
        tb4.setOnClickListener(this);
        tb5.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        ToggleButton tb = (ToggleButton) v;


        RadioGroup llLayout = (RadioGroup) tb.getParent();

        for(int i=0; i<((ViewGroup)llLayout).getChildCount(); ++i) {
            View nextChild = ((ViewGroup)llLayout).getChildAt(i);

            ToggleButton cb2=(ToggleButton) nextChild;

            if(nextChild instanceof ToggleButton && nextChild.getId()==tb.getId() ){
                //cb2.setChecked(false);
            }else if (nextChild instanceof ToggleButton && nextChild.getId()!=tb.getId() ){
                cb2.setChecked(false);
            }
        }

    }
}

你可以download my git repo.

【讨论】:

  • 我所做的只是将我的可绘制对象更改为您的,并且它可以工作。这很奇怪。你能告诉我我的drawable有什么问题吗?这样我就可以知道我做错了什么?谢谢。
  • 没什么.. 你只要看看我的 button_style 和 onClick 视图... 学习调试一下..
猜你喜欢
  • 2020-06-02
  • 2015-02-25
  • 1970-01-01
  • 2016-01-03
  • 2013-05-23
  • 1970-01-01
  • 2018-02-06
  • 2019-12-29
  • 2017-12-23
相关资源
最近更新 更多