android中互斥的控件,android控件之radiobutton与radiogroup -凯发k8官方网
radiobutton 是一个单选控件,在一个radiogroup中,各个radiobutton是互斥的
xml文件:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativepackage}.${activityclass}" >
android:id="@ id/radiogroup1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
>
android:id="@ id/radiobutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignparentleft="true"
android:layout_margintop="42dp"
android:text="radiobutton" />
android:id="@ id/radiobutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignparentleft="true"
android:layout_below="@ id/radiobutton1"
android:text="radiobutton" />
android:id="@ id/radiobutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignparentleft="true"
android:layout_below="@ id/radiobutton2"
android:text="radiobutton" />
使用oncheckedchangelistener监听:
package com.example.textview;
import android.app.activity;
import android.os.bundle;
import android.widget.radiobutton;
import android.widget.radiogroup;
import android.widget.radiogroup.oncheckedchangelistener;
public class mainactivity extends activity {
private radiogroup radiogroup1=null;
private radiobutton radiobutton1=null;
private radiobutton radiobutton2=null;
private radiobutton radiobutton3=null;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
radiogroup1=(radiogroup)findviewbyid(r.id.radiogroup1);
radiobutton1=(radiobutton)findviewbyid(r.id.radiobutton1);
radiobutton2=(radiobutton)findviewbyid(r.id.radiobutton2);
radiobutton3=(radiobutton)findviewbyid(r.id.radiobutton3);
radiogroup1.setoncheckedchangelistener(listener);
}
private oncheckedchangelistener listener = new oncheckedchangelistener() {
@override
public void oncheckedchanged(radiogroup group, int checkedid) {
if(checkedid==radiobutton1.getid())
{
system.out.println("button 1");
}
else if(checkedid==radiobutton2.getid())
{
system.out.println("button 2");
}
else if(checkedid==radiobutton3.getid())
{
system.out.println("button 3");
}
}
};
}
与50位技术专家面对面20年技术见证,附赠技术全景图总结
以上是凯发k8官方网为你收集整理的android中互斥的控件,android控件之radiobutton与radiogroup的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: webview 修改html,使用自定义