当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现单项、多项选择操作

Android实现单项、多项选择操作

2019年07月24日  | 移动技术网IT编程  | 我要评论

蛋糕的做法大全视频,乒乓球教学视频,暗之末裔

本文实例为大家分享了android实现单项、多项选择操作的相关代码,供大家参考,具体内容如下

1、单项选择
1.1.布局

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 tools:context="com.rj141.sb.kongjian.dateactivity"> 
 
 
 <textview 
  android:layout_marginleft="10dp" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="2+3=" 
  android:textsize="22dp" 
  /> 
 
 <radiogroup 
  android:layout_marginleft="20dp" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"> 
  <radiobutton 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="a.2" 
   android:id="@+id/rb1" 
   /> 
  <radiobutton 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="b.3" 
   android:id="@+id/rb2" 
   /> 
  <radiobutton 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="c.4" 
   android:id="@+id/rb3" 
   /> 
  <radiobutton 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="d.5" 
   android:id="@+id/rb4" 
   /> 
 </radiogroup> 
 
 <button 
  android:id="@+id/submit" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="提交"/> 
</linearlayout>

 1.2.java文件

public class singchoose extends appcompatactivity { 
 private button btn; 
 private radiobutton rbd; 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.sing_choose); 
 
  rbd= (radiobutton) this.findviewbyid(r.id.rb4); 
  btn= (button) this.findviewbyid(r.id.submit); 
  btn.setonclicklistener(new view.onclicklistener() { 
   @override 
   public void onclick(view v) { 
    if(rbd.ischecked()){ 
     toast.maketext(singchoose.this,"正确,请加五分",toast.length_short).show(); 
    } 
    else { 
     toast.maketext(singchoose.this,"错误,请减五分",toast.length_short).show(); 
    } 
   } 
  }); 
 } 
} 

效果图:

2多项选择
2.1.布局

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 tools:context="com.rj141.sb.kongjian.checkchoose"> 
 
 <textview 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:textsize="20dp" 
  android:text="你喜欢下列哪些物品?" 
  /> 
 
 <checkbox 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="巧克力" 
  android:id="@+id/cb1" /> 
 
 <checkbox 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="冰淇淋" 
  android:id="@+id/cb2" /> 
 
 <checkbox 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="蛋糕" 
  android:id="@+id/cb3" /> 
 
 <checkbox 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="啤酒" 
  android:id="@+id/cb4" /> 
 
 <checkbox 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="肉" 
  android:id="@+id/cb5" /> 
 
 <textview 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:textsize="18dp" 
  android:id="@+id/tv" /> 
 
</linearlayout> 

2.2.java文件

public class checkchoose extends appcompatactivity implements compoundbutton.oncheckedchangelistener { 
 
 private checkbox cb1,cb2,cb3,cb4,cb5; 
 private textview tv; 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.check_choose); 
 
  tv= (textview) this.findviewbyid(r.id.tv); 
  cb1= (checkbox) this.findviewbyid(r.id.cb1); 
  cb2= (checkbox) this.findviewbyid(r.id.cb2); 
  cb3= (checkbox) this.findviewbyid(r.id.cb3); 
  cb4= (checkbox) this.findviewbyid(r.id.cb4); 
  cb5= (checkbox) this.findviewbyid(r.id.cb5); 
  cb1.setoncheckedchangelistener(this); 
  cb2.setoncheckedchangelistener(this); 
  cb3.setoncheckedchangelistener(this); 
  cb4.setoncheckedchangelistener(this); 
  cb5.setoncheckedchangelistener(this); 
 } 
 
 @override 
 public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { 
  string str="您喜欢:"; 
  if(cb1.ischecked()){ 
   str+=cb1.gettext()+","; 
  } 
  if(cb2.ischecked()){ 
   str+=cb2.gettext()+","; 
  } 
  if(cb3.ischecked()){ 
   str+=cb3.gettext()+","; 
  } 
  if(cb4.ischecked()){ 
   str+=cb4.gettext()+","; 
  } 
  if(cb5.ischecked()){ 
   str+=cb5.gettext()+","; 
  } 
  tv.settext(str); 
 } 
} 

效果图:

以上就是本文的全部内容,希望对大家学习android软件编程有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网