使用ListView和RadioGroup做单选题,并且将所选选项保存起来

原创 2016年05月31日 09:12:17

1.建立一个QItem类,来实现每道题的构造方法
<span style="font-size:18px;"><pre name="code" class="java"><span style="font-size:18px;">	class QItem{
		//问题
		private String text;
		//选项
		private String A;
		private String B;
		private String C;
		//正确选项按钮Id
		private int answer;
		public String getText() {
			return text;
		}
		public String getA() {
			return A;
		}
		public String getB() {
			return B;
		}
		public String getC() {
			return C;
		}
		public int getAnswer() {
			return answer;
		}
		
		public QItem(String text, String a, String b, String c, int answer) {
			super();
			this.text = text;
			A = a;
			B = b;
			C = c;
			this.answer = answer;
		}
		public QItem() {
			super();
		}
	}</span></span>

Item的布局方式

<pre name="code" class="html"><span style="font-size:18px;"><?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" 
    android:id="@+id/exam_layout">

    <TextView
        android:id="@+id/examquestion_tvId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="question"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RadioGroup
        android:id="@+id/exam_xuanzerp_Id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/exam_A"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="A" />

        <RadioButton
            android:id="@+id/exam_B"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="B" />

        <RadioButton
            android:id="@+id/exam_C"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="C" />
    </RadioGroup>

</LinearLayout></span>
<h1>Activity</h1><div><pre name="code" class="html"><span style="font-size:18px;"><RelativeLayout 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"
    tools:context=".ExamActivity" >


    <ScrollView
        android:id="@+id/exam_scrId"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none" >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <!-- 考试分数,刚开始设置显示模式为gone -->
            <TextView 
                android:id="@+id/exam_fenshutvId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="aaa"
                android:textSize="32sp"
                android:visibility="gone"/>
			<!-- 根据考试是否过关显示不同的图片,刚开始设置显示模式为gone -->
            <ImageView
                android:id="@+id/chengji_imgId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/ic_launcher"
                android:visibility="gone" />


            <LinearLayout
                android:gravity="center_horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
				<!-- 根据考试分数的档位不同最后显示不同图片,刚开始设置显示模式为gone -->
                <ImageView
                    android:id="@+id/exam_btnlogoId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/ic_launcher"
                    android:visibility="gone" />
				<!-- 考试完成后点击退出 ,刚开始设置显示模式为gone-->
                <Button
                    android:id="@+id/exam_finishId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:background="#ffffffff"
                    android:onClick="doClick"
                    android:text="考试结束"
                    android:visibility="gone" />
            </LinearLayout>
			<!-- 答题区域,考试完成后可设置显示模式为gone-->
			<!-- 如果还要翻看一定要重写ListView,不然与ScrollView合用会导致ListView的高度很小 -->
            <com.example.elts.MyListView
                android:id="@+id/exam_lvId"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:descendantFocusability="blocksDescendants" />
        </LinearLayout>
    </ScrollView>


</RelativeLayout>
</span>

ListView重写方法

<span style="font-size:18px;">import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class MyListView extends ListView {

	public MyListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public MyListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public MyListView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		heightMeasureSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2 ,MeasureSpec.AT_MOST);

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
}</span>

设置问题,listView的适配器,以及获得选择按钮的保存部分代码

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.elts.R;

public class ExamActivity extends Activity {

	private ListView lv;
	private ImageView img;
	private ImageView btnlogo;
	private ScrollView scr;
	private Button btn;
	private TextView chengji;
	private int count=0;
	//问题部分
	public List<QItem> question=new ArrayList<QItem>();
	{
		question.add(new QItem("1.可以闯红灯吗","A.可以","B.不可以","C.不知道",R.id.exam_B));
		question.add(new QItem("2.撞了人怎么办","A.再撞一次","B.逃逸","C.下车查看情况并拨打120",R.id.exam_C));
		question.add(new QItem("3.开车可以喝什么酒","A.不能喝","B.啤酒","C.白酒",R.id.exam_A));
		question.add(new QItem("4.开车能用手机吗","A.可以玩游戏","B.不能用手机","C.可以打电话",R.id.exam_B));
		question.add(new QItem("5.没有驾照可以开车吗","A.不可以","B.不知道","C.可以",R.id.exam_A));
		question.add(new QItem("6.可以闯红灯吗","A.可以","B.不可以","C.不知道",R.id.exam_B));
		question.add(new QItem("7.撞了人怎么办","A.再撞一次","B.逃逸","C.下车查看情况并拨打120",R.id.exam_C));
		question.add(new QItem("8.开车可以喝什么酒","A.不能喝","B.啤酒","C.白酒",R.id.exam_A));
		question.add(new QItem("9.开车能用手机吗","A.可以玩游戏","B.不能用手机","C.可以打电话",R.id.exam_B));
		question.add(new QItem("10.没有驾照可以开车吗","A.不可以","B.不知道","C.可以",R.id.exam_A));

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_exam);
		setActionbar();
		setListView();
		scr=(ScrollView) findViewById(R.id.exam_scrId);
		scr.smoothScrollTo(0, 0);

	}
	private Menu menu;
<span style="white-space:pre">	</span>//完成后点击返回按钮的点击事件
	public void doClick(View view) {
		startActivity(new Intent(this,BeforeExamActivity.class));
	}
<span style="white-space:pre">	</span>//定义一个数组用来保存每个item所选的按钮id
	private int [] answer=new int [question.size()];
	private void setListView() {
		// TODO Auto-generated method stub
		lv=(ListView) findViewById(R.id.exam_lvId);
<span style="white-space:pre">		</span>//重写getview方法
		ArrayAdapter<QItem> adapter =new ArrayAdapter<QItem>(this, 
				R.layout.exam_listview,question)
		{


			@Override
			public View getView(final int position, View convertView,
					ViewGroup parent) {
				QItem item=getItem(position); 
				View v=View.inflate(ExamActivity.this,R.layout.exam_listview,null);
				TextView question=(TextView) v.findViewById(R.id.examquestion_tvId);

				RadioButton A=(RadioButton) v.findViewById(R.id.exam_A);
				RadioButton B=(RadioButton) v.findViewById(R.id.exam_B);
				RadioButton C=(RadioButton) v.findViewById(R.id.exam_C);
				RadioGroup rp=(RadioGroup) v.findViewById(R.id.exam_xuanzerp_Id);

				question.setText(item.getText());
				A.setText(item.getA());
				B.setText(item.getB());
				C.setText(item.getC());
<span style="white-space:pre">				</span>//设置选项radiogroup的监听器用来保存选择的id并保存到数组内
				rp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						int id=group.getCheckedRadioButtonId();
						answer[position]=id;
					}
				});
				return v;
			}
		};
		lv.setAdapter(adapter); 
	}
<span style="white-space:pre">	</span>//自定义<span style="font-family: Arial, Helvetica, sans-serif;">Actionbar</span>

	private void setActionbar() {
		// TODO Auto-generated method stub
		//1.获得actionBar(注意版本要求),设置背景
		ActionBar bar=getActionBar();

		bar.setDisplayUseLogoEnabled(true);
		//设置homebutton可点击
		bar.setDisplayShowHomeEnabled(false);
		//			//隐藏title
		bar.setDisplayShowTitleEnabled(false);
		//2.设置actionBar的导航模式
		bar.setNavigationMode(ActionBar.DISPLAY_SHOW_CUSTOM);
		bar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_background_01));
		//3.设置actionBar显示自定义view
		bar.setDisplayShowCustomEnabled(true);
		//4.设置actionBar的自定义view
		View mCustomeView=View.inflate(this,R.layout.exam_tittle,null);
		//			bar.setCustomView(mCustomeView);
		bar.setCustomView(mCustomeView,
				new LayoutParams(
						LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT,
						Gravity.CENTER));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.exam, menu);
		this.menu=menu;
		return true;
	}
<span style="white-space:pre">	</span>//设置actionbar的按钮点击事件
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		// TODO Auto-generated method stub
		if(item.getItemId()==R.id.exam_jiaojuanId){
			img=(ImageView) findViewById(R.id.chengji_imgId);
			btnlogo=(ImageView) findViewById(R.id.exam_btnlogoId);
			btn=(Button)findViewById(R.id.exam_finishId);
			chengji=(TextView)findViewById(R.id.exam_fenshutvId);
			new AlertDialog.Builder(this).
			setTitle("提示").setMessage("确定要交卷吗").
			setPositiveButton("确定交卷", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					/**设置scrollview到顶端*/
					/**设置actionbar上的取消考试和交卷按钮组隐藏*/
					/**设置分数,分数的图片以及完成考试按钮显示*/
					/**设置答题的listview隐藏(非必要)*/
					scr.smoothScrollTo(0, 0);
					menu.setGroupVisible(R.id.exam_groupId, false);
					img.setVisibility(View.VISIBLE);
					btn.setVisibility(View.VISIBLE);
					chengji.setVisibility(View.VISIBLE);
					btnlogo.setVisibility(View.VISIBLE);
					lv.setVisibility(View.GONE);

					/**计分方法*/
					for(int i=0;i<question.size();i++){
						if(answer[i]==((question.get(i)).getAnswer())){
							count++;
						}
					}
						
					/**评分规则*/
					double fenshu=100*count/question.size();
					chengji.setText("您的成绩:"+fenshu+"分");
					if (fenshu<70){
						chengji.setTextColor(Color.RED);
						img.setVisibility(View.VISIBLE);
						btn.setText("勿弃疗,点我返回");
						btnlogo.setImageResource(R.drawable.question_wrong_car);
						img.setImageResource(R.drawable.exam_fenshu_e);}
					if (70<=fenshu && fenshu<80){
						chengji.setTextColor(Color.GREEN);
						img.setVisibility(View.VISIBLE);
						btn.setText("何弃疗,点我返回");
						btnlogo.setImageResource(R.drawable.question_right_car);
						img.setImageResource(R.drawable.exam_fenshu_d);}
					if (80<=fenshu && fenshu<90){
						chengji.setTextColor(Color.GREEN);
						img.setVisibility(View.VISIBLE);
						btn.setText("继续努力,点我返回");
						btnlogo.setImageResource(R.drawable.question_right_car);
						img.setImageResource(R.drawable.exam_fenshu_c);}
					if (90<=fenshu && fenshu<100){
						chengji.setTextColor(Color.GREEN);
						img.setVisibility(View.VISIBLE);
						btn.setText("再接再厉,点我返回");
						btnlogo.setImageResource(R.drawable.question_right_car);
						img.setImageResource(R.drawable.exam_fenshu_b);}
					if (fenshu==100){
						chengji.setTextColor(Color.GREEN);
						img.setVisibility(View.VISIBLE);
						btn.setText("继续保持,点我返回");
						btnlogo.setImageResource(R.drawable.question_right_car);
						img.setImageResource(R.drawable.exam_fenshu_a);}
				}
			}).setNegativeButton("取消", null).
			create().show();
			return true;
		}
		if(item.getItemId()==R.id.exam_quxiaoId){
			new AlertDialog.Builder(this).
			setTitle("提示").
			setMessage("您确定取消考试吗?").
			setPositiveButton("确定", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			}).
			setNegativeButton("取消", null).
			create().show();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

menu文件夹下activity的设置

<span style="font-size:18px;"><menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <group
        android:id="@+id/exam_groupId"
        android:visible="true" >
        <item
            android:id="@+id/exam_jiaojuanId"
            android:icon="@drawable/icon_examin_jiaojuan"
            android:orderInCategory="100"
            android:showAsAction="always"
            android:title="交卷"/>
        <item
            android:id="@+id/exam_quxiaoId"
            android:icon="@drawable/icon_examin_qxpaichu"
            android:orderInCategory="99"
            android:showAsAction="always"
            android:title="取消考试"/>
    </group>

</menu></span>










版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

帮你全面彻底搞定MATERIAL DESIGN的学习笔记

自从Material design发布以来,可乐橙就在一直收集相关素材与资源,研究别人的作品。这套设计风格非常鲜明,带有浓郁的Google式严谨和理性哲学,深得我心。实际上,光是研究素材和别人作品,就...

在Java中,如何把一个程序的结果需要保存起来,供下一个程序使用的方式

1:最方便的、永久保存的数据库方式。 一个程序执行完后,把结果存入到数据库中,下一个程序启动初始化后再去数据库中查找是否已经存在相应的信息。这种方式的好处:存储方便,只需借助数据库的存储即可,永久保...

使用Jquery动态的实现增加/删除单选题

最近在做一个考试系统,在动态添加单项题这里遇到这个问题,进行简单的记录和总结

如何将tomcat控制台输出的内容直播用日志文件保存起来

http://nlslzf.iteye.com/blog/568665 http://topic.csdn.net/u/20091113/14/01b60216-7111-4cfd-8964...

如何将tomcat控制台输出的内容直播用日志文件保存起来

http://topic.csdn.net/u/20091113/14/01b60216-7111-4cfd-8964-dde15a7f62d8.html  在项目中的classes 中新建立一个l...

flash做的单选题

  • 2011-12-14 16:32
  • 195KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)