[英]click toggle button programmatically in android
hello i have a toggle button like this : 你好,我有一个像这样的切换按钮:
<RadioGroup android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<ToggleButton android:id="@+id/detailed_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/detailed_view"
android:textOff="@string/detailed_view"
android:textOn="@string/detailed_view"
android:onClick="viewChange" />
<ToggleButton android:id="@+id/main_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_view"
android:textOff="@string/main_view"
android:textOn="@string/main_view"
android:checked="true"
android:onClick="viewChange"
/>
</RadioGroup>
and i have the function viewChange like this: 我有这样的功能viewChange:
public void viewChange(View v){
int id=v.getId();
if(id==R.id.main_view){
//do some thing with main view
}else if(id==R.id.detailed_view){
do something with detailed view
}
}
Now i need to call this function ViewChange(View v) and set the parameter v programatically, i means that i want to click this toggle button programatically and specify which view i want to click. 现在,我需要调用此函数ViewChange(View v)并以编程方式设置参数v,这意味着我想以编程方式单击此切换按钮并指定要单击的视图。
You can programmatically change the state of the ToggleButton
by using the setChecked
method: 您可以使用setChecked
方法以编程方式更改ToggleButton
的状态:
toggleButton.setChecked(true or false)
More information on the ToggleButton
may be found in the API reference: http://developer.android.com/guide/topics/ui/controls/togglebutton.html 可以在API参考中找到有关ToggleButton
更多信息: http : //developer.android.com/guide/topics/ui/controls/togglebutton.html
make use of 利用
.setChecked()
property. 属性。 Ref http://developer.android.com/reference/android/widget/ToggleButton.html#setChecked%28boolean%29 引用http://developer.android.com/reference/android/widget/ToggleButton.html#setChecked%28boolean%29
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.