gpt4 book ai didi

java - 我的可绘制对象会干扰我的按钮状态吗?

转载 作者:行者123 更新时间:2023-11-30 02:35:57 25 4
gpt4 key购买 nike

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

所以像这样:

布局.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>

适配器中的代码 fragment :

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);

}
}
}
}
});

对我拥有的每个按钮重复上述操作。在本例中为按钮零和一。

可绘制:

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

我想要的行为:

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

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

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

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

最佳答案

我想你正在寻找这个:

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.

关于java - 我的可绘制对象会干扰我的按钮状态吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26554764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com