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

 更新时间:2016年04月29日 15:29:49   投稿:lijiao  
这篇文章主要介绍了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中View跟随手指滑动效果的实例代码

    Android中View跟随手指滑动效果的实例代码

    这篇文章主要介绍了Android中View跟随手指滑动效果的实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • android界面布局之实现文本块布局效果示例

    android界面布局之实现文本块布局效果示例

    这篇文章主要介绍了android实现文本块布局效果示例,需要的朋友可以参考下
    2014-04-04
  • Android应用的多语言支持的实现方法

    Android应用的多语言支持的实现方法

    本篇文章主要介绍了Android应用的多语言支持的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Android滚动条广告实现代码示例

    Android滚动条广告实现代码示例

    本篇文章主要介绍了Android滚动条广告实现代码示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • android WebView组件使用总结

    android WebView组件使用总结

    浏览器控件是每个开发环境都具备的,这为马甲神功提供了用武之地,windows的有webbrowser,android和ios都有webview;本篇主要介绍android的webview之强大,感兴趣的朋友可以研究下
    2012-12-12
  • Android获取应用程序大小的方法

    Android获取应用程序大小的方法

    这篇文章主要介绍了Android获取应用程序大小的方法,有需要的朋友可以参考一下
    2014-01-01
  • Android APK反编译技巧深入讲解

    Android APK反编译技巧深入讲解

    这篇文章主要给大家介绍了关于Android APK反编译技巧的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • Android播放视频的三种方式

    Android播放视频的三种方式

    这篇文章主要为大家详细介绍了Android播放视频的三种方式,使用其自带的播放器、VideoView、MediaPlayer类和SurfaceView来实现,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android中Notification的用法汇总

    Android中Notification的用法汇总

    这篇文章主要介绍了Android中Notification的用法汇总的相关资料,需要的朋友可以参考下
    2016-01-01
  • Android之ProgressBar即时显示下载进度详解

    Android之ProgressBar即时显示下载进度详解

    这篇文章主要为大家详细介绍了Android之ProgressBar即时显示下载进度,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09

最新评论