- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
现在,我有一个单选组作为父组,切换按钮作为子组。
所以像这样:
布局.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);
}
}
}
}
关于java - 我的可绘制对象会干扰我的按钮状态吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26554764/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!