Android开发————RaidoGroup学习笔记
来源:互联网 发布:mac os 10.7升级包 编辑:程序博客网 时间:2024/06/07 07:20
实例代码测试:
主体显示Activity代码:
package com.person.radiogrouptest;import android.app.ListActivity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends ListActivity {// 创建一个适配器,存储的内容为ListCellDataprivate ArrayAdapter<ListCellData> adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 创建适配器,在主布局中放入android提供的list,list存储的内容为ListCellData类adapter = new ArrayAdapter<ListCellData>(this,android.R.layout.simple_list_item_1);// 该布局绑定到主布局上setListAdapter(adapter);// 将添加进来的组件进行设置,名称设置为RaidoGroup,消息是从MainActivity送到AtyUsingRadioGroup类中// 进入ListCellData之后将相关信息进行保存,用构建函数进行生成该类的实例adapter.add(new ListCellData(this, "RadioGroup", new Intent(this,AtyUsingRadioGroup.class)));}// 当点击名称为RadioGroup的Item的时候会触发该事件protected void onListItemClick(ListView l, View v, int position, long id) {// 将点击该Item的位置信息反馈出来ListCellData data = adapter.getItem(position);// 将该Item应该触发出来的Activity进行显示开启data.startActivity();super.onListItemClick(l, v, position, id);}}
ListView组件中小组件的定义类:
package com.person.radiogrouptest;import android.content.Context;import android.content.Intent;public class ListCellData {private Context context = null;private Intent relatedIntent = null;private String controlName = "";public Intent getRelatedIntent() {return relatedIntent;}public Context getContext() {return context;}public ListCellData(Context context, String controlName,Intent relatedIntent) {this.controlName = controlName;this.context = context;this.relatedIntent = relatedIntent;}public String getControlName() {return controlName;}public void startActivity() {getContext().startActivity(getRelatedIntent());}@Overridepublic String toString() {// TODO Auto-generated method stubreturn getControlName();}}
用于显示小组件的Activity的建立代码:
package com.person.radiogrouptest;import android.app.Activity;import android.app.AlertDialog;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RadioButton;//显示RadioGroup的Activitypublic class AtyUsingRadioGroup extends Activity {// 创建Button和RadioButton组件private RadioButton r1;private Button ok_buttn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.aty_using_radiogroup);// 将RadioButton的第一个Button引入进行相关操作r1 = (RadioButton) findViewById(R.id.r1);// 将按钮ok_buttn引入进行相关操作ok_buttn = (Button) findViewById(R.id.ok);// 设置该按钮进行事件监听,对按钮按下的动作进行相关的回应ok_buttn.setOnClickListener(new OnClickListener() {public void onClick(View v) {// 如果第一个RadioButton选中,并且进行了提交if (r1.isChecked()) {// 输出判断的结果// 判断:// 回答正确//// 关闭按钮new AlertDialog.Builder(AtyUsingRadioGroup.this).setTitle("判断").setMessage("回答正确").setPositiveButton("关闭", null).show();// new AlertDialog.Builder(AtyUsingRadioGroup.this)// .setTitle("判断").setMessage("回答正确").setPositiveButton("关闭",// new DialogInterface.OnClickListener() {//// @Override// public void onClick(DialogInterface dialog, int which) {// // TODO Auto-generated method stub//// }// }).show();} else {// 如果没有选中// 判断:// 回答错误//// 关闭按钮// 其中setPositiveButton("该按钮的名称",传入的侦听动作(设置成NUll表示没有对应的操作))// Builder(context),传入的是AtyUsingRadioGroup.this,表示传入的是该类的实例Actitvity// title是设置ALtert对话框的标题new AlertDialog.Builder(AtyUsingRadioGroup.this).setTitle("判断").setMessage("回答错误").setPositiveButton("关闭", null).show();}}});}}
相关布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你是帅哥吗?" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/r1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="是" /> <RadioButton android:id="@+id/r2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="不是" /> <RadioButton android:id="@+id/r3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="不知道" /> </RadioGroup> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交" /></LinearLayout>
进行活动注册的XML代码:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.person.radiogrouptest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.person.radiogrouptest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="AtyUsingRadioGroup"></activity> </application></manifest>
最终显示出来的图像
点击RaidoGroup之后将会显示出来:
0 0
- Android开发————RaidoGroup学习笔记
- android开发学习笔记——权限
- Android学习笔记(02)——搭建Android开发环境
- Android开发————ListView学习笔记
- Android开发————CheckBox学习笔记
- Android开发————DatePicker学习笔记
- Android开发————TimePicker学习笔记
- Android开发————Notification学习笔记
- Android开发————后退按键学习笔记
- Android开发————国际化学习笔记
- Android开发————SharedPreferences学习笔记
- Android开发————BaseAdapter学习笔记
- Android开发————Alarm学习笔记
- Android开发————Notification学习笔记
- Android 学习笔记(一)——开发环境搭建
- Android学习笔记(二)——开发环境的搭建
- 工作学习笔记——Alljoyn技术、初试Android开发
- Android开发学习笔记——TabHost实例
- 4.PHP内核探索:单进程SAPI生命周期
- Java程序员常用工具类库
- iOS 7系列译文:忘记NSURLConnection,拥抱NSURLSession吧!
- ListCtrl----虚拟列表的用法
- oc中如何调用c++的方法
- Android开发————RaidoGroup学习笔记
- 2058 The sum problem
- Adaptive Support-weighted Approach for Correspondence Search
- Eclise中SVN插件安装和使用
- ADO.net实体框架访问不到数据库最新数据问题
- thinkPHP中的三大自动
- 关于对齐的总结
- Java开源内容管理CMS系统J4CMS的几个样式
- Web 开发中很实用的10个效果【附源码下载】