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

Android实现单项、多项选择 *** 作,第1张

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

本文实例为大家分享了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软件编程有所帮助。

总结

以上是内存溢出为你收集整理的Android实现单项、多项选择 *** 作全部内容,希望文章能够帮你解决Android实现单项、多项选择 *** 作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1149334.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存