初学android,写了个选择题的页面,困惑我好几天了,哪位看看如何实现这个功能,先简单描述一下。
本身页面由一个pagerTab的两个viewpager组成,两个viewpager左右滑动,在右边的viewpager上加载了一个listview,listview的每个item由四个radiobutton和一个textview组成,功能就是如何实现四个radiobutton的单选。
testActivity
package com.exam.kyztbd;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.WindowManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class testActivity extends Activity{
private View question, answer,checkans;
private ViewPager viewPager;
private PagerTabStrip pagerTabStrip;
private List<View> viewList;
private List<String> titleList;
private Button prevbutton;
private Button checkanswerbutton;
private Button setbutton;
private Button addfavorbutton;
private Button nextbutton;
private TextView articletextview;
private ListView lv; 
private SimpleAdapter sa;
private DBHelper helper;
private ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
private HashMap<String, Object> map = new HashMap<String, Object>();
    int initial_quesnum;
    int articletotalnum;
    SharedPreferences prex;
    Cursor c;
    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.textpaper);
initView();
//读取数据库,获取文件目录
prex = getSharedPreferences("init_flag",MODE_WORLD_READABLE);//获取当前题目号
initial_quesnum=prex.getInt("quesnum_flag",1); //获取当前题目号
displayquestion();

prevbutton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
                if(initial_quesnum>1)  
                {
                 initial_quesnum=initial_quesnum-1;
                prex = getSharedPreferences("init_flag",MODE_WORLD_WRITEABLE);
              SharedPreferences.Editor editor = prex.edit();
              editor.putInt("quesnum_flag", initial_quesnum);
              editor.commit();
              displayquestion();                
                }
                else{
                 //提示已是第一题
                 Toast.makeText(getApplicationContext(), "已经是第一题了",Toast.LENGTH_SHORT).show();
                }


}
});
checkanswerbutton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
               checkanswer();


}
});
setbutton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}
});
addfavorbutton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}
});
nextbutton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
  if(initial_quesnum<articletotalnum)  
                {
                 initial_quesnum=initial_quesnum+1;
                prex = getSharedPreferences("init_flag",MODE_WORLD_WRITEABLE);
              SharedPreferences.Editor editor = prex.edit();
              editor.putInt("quesnum_flag", initial_quesnum);
              editor.commit(); 
              displayquestion();
                   
                }
                else{
                
                }

}
});




}
    private void initView() { viewPager = (ViewPager) findViewById(R.id.viewpager);
pagerTabStrip=(PagerTabStrip) findViewById(R.id.pagertab);
prevbutton=(Button)findViewById(R.id.prevbtn);
checkanswerbutton=(Button)findViewById(R.id.checkanswerbtn);
setbutton=(Button)findViewById(R.id.setbtn);
addfavorbutton=(Button)findViewById(R.id.addfavorbtn);
nextbutton=(Button)findViewById(R.id.nextbtn);
pagerTabStrip.setDrawFullUnderline(false);
pagerTabStrip.setTextSpacing(50);
question = findViewById(R.layout.layout1);
answer = findViewById(R.layout.layout2);
getLayoutInflater();
LayoutInflater lf = LayoutInflater.from(this);
question = lf.inflate(R.layout.layout1, null);
answer = lf.inflate(R.layout.layout2, null);
viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
viewList.add(question);
viewList.add(answer);
titleList = new ArrayList<String>();// 每个页面的Title数据
titleList.add("英文短文");
titleList.add("选择题干");
articletextview=(TextView)question.findViewById(R.id.article);
lv=(ListView)answer.findViewById(R.id.question1);
PagerAdapter pagerAdapter = new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
} @Override
public int getCount() { return viewList.size();
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView(viewList.get(position)); } @Override
public int getItemPosition(Object object) { return super.getItemPosition(object);
} @Override
public CharSequence getPageTitle(int position) {

               
return titleList.get(position);
} @Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));    return viewList.get(position);

} };
viewPager.setAdapter(pagerAdapter);
}
    
    
    
    private void displayquestion()    {
     helper = new DBHelper(this);//打开数据
c= helper.queryArticleTbl();//查询所有文章列表
articletotalnum = c.getCount(); //获取文章(题目)总数
c=helper.queryByArticleNum(initial_quesnum);//按照当前题目号查询文章路径
String article_txt_name=c.getString(2); //获取文章路径
setbutton.setText(initial_quesnum+"/"+articletotalnum);//按钮显示“当前题目号/题目总数”
    File fis = new File(Environment.getExternalStorageDirectory() + "/kyztbd/db/"+article_txt_name);//获取保存在SD卡中文章的路径
    String str = null;
    try {
        InputStream is = new FileInputStream(fis);//读取SD卡中存储的文章并在textview中显示出来
        InputStreamReader input = new InputStreamReader(is, "UTF-8");
        BufferedReader reader = new BufferedReader(input);
        articletextview.setText("");
        while ((str = reader.readLine()) != null) {
         articletextview.append(str);
         articletextview.append("\n");         
        }
        reader.close();     } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    c=helper.queryByQuestion(initial_quesnum);//根据题目号查询问题
    data.clear();
    while(c.moveToNext())
    {     
     map = new HashMap<String,Object>();
     map.put("_id", c.getString(c.getColumnIndex("_id")));
map.put("articlenumber", c.getString(c.getColumnIndex("articlenumber")));
map.put("questionnumber", c.getString(c.getColumnIndex("questionnumber")));
map.put("question_txt", c.getString(c.getColumnIndex("question_txt")));
map.put("answerA_txt", c.getString(c.getColumnIndex("answerA_txt")));
     map.put("answerB_txt", c.getString(c.getColumnIndex("answerB_txt")));
map.put("answerC_txt", c.getString(c.getColumnIndex("answerC_txt")));
map.put("answerD_txt", c.getString(c.getColumnIndex("answerD_txt")));
data.add(map);
    }
    
    String[] from = new String[] {"questionnumber","question_txt","answerA_txt","answerB_txt","answerC_txt","answerD_txt"};
    int[] to = new int[]{R.id.questionnum_questiondetail,R.id.questiontxt_questiondetail,R.id.answerA_questiondetail,
     R.id.answerB_questiondetail,R.id.answerC_questiondetail,R.id.answerD_questiondetail};
    sa = new SimpleAdapter(this,data,R.layout.questiondetail,from,to);
    lv.setAdapter(sa);
    }

解决方案 »

  1.   

        private void checkanswer(){
         c=helper.queryByQuestion(initial_quesnum);//根据题目号查询问题
         data.clear();
         String note="";
        while(c.moveToNext()){
         map = new HashMap<String,Object>();
         map.put("_id", c.getString(c.getColumnIndex("_id")));
    map.put("articlenumber", c.getString(c.getColumnIndex("articlenumber")));
    map.put("questionnumber", c.getString(c.getColumnIndex("questionnumber")));
    map.put("question_txt", c.getString(c.getColumnIndex("question_txt")));
    map.put("answerA_txt", c.getString(c.getColumnIndex("answerA_txt")));
         map.put("answerB_txt", c.getString(c.getColumnIndex("answerB_txt")));
    map.put("answerC_txt", c.getString(c.getColumnIndex("answerC_txt")));
    map.put("answerD_txt", c.getString(c.getColumnIndex("answerD_txt")));
    //解决sqlite读出中文乱码问题
    byte[] re = c.getBlob(c.getColumnIndex("re_txt"));
    try {
    note = new String(re,"GBK");
    } catch (UnsupportedEncodingException e) {
       e.printStackTrace();
    }
    map.put("answerKey_txt", "正确答案为:"+c.getString(c.getColumnIndex("answerKey_txt"))+"\n"+note);
    data.add(map);

        }     String[] from = new String[] {"questionnumber","question_txt","answerA_txt","answerB_txt","answerC_txt","answerD_txt","answerKey_txt"};
        int[] to = new int[]{R.id.questionnum_questiondetail,R.id.questiontxt_questiondetail,R.id.answerA_questiondetail,
         R.id.answerB_questiondetail,R.id.answerC_questiondetail,R.id.answerD_questiondetail,R.id.answerKey_questiondetail};
        sa = new SimpleAdapter(this,data,R.layout.questiondetail,from,to);
         lv.setAdapter(sa);     
        }   
    }textpaper.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >    <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="wrap_content"
            android:layout_height="586dp"
            android:layout_gravity="center" >
                  
            <android.support.v4.view.PagerTabStrip  
                android:id="@+id/pagertab"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_gravity="top"
                android:background="#057AFE"
              />     
        </android.support.v4.view.ViewPager>    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:background="#057AFE" >
            <Button
                android:id="@+id/prevbtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#00000000"
                android:drawableTop="@drawable/icon_previous1"
                android:text="@string/prevtxt"
                android:textColor="#FFFFFFFF" />
            <Button
                android:id="@+id/checkanswerbtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#00000000"
                android:drawableTop="@drawable/icon_mode_answer1"
                android:text="@string/checkanswertxt"
                android:textColor="#FFFFFFFF" />
            <Button
                android:id="@+id/setbtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#00000000"
                android:drawableTop="@drawable/icon_set_n"
                android:text="@string/settxt"
                android:textColor="#FFFFFFFF" />
            <Button
                android:id="@+id/addfavorbtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#00000000"
                android:drawableTop="@drawable/icon_favor1"
                android:text="@string/addfavortxt"
                android:textColor="#FFFFFFFF" />
            <Button
                android:id="@+id/nextbtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#00000000"
                android:drawableTop="@drawable/icon_next1"
                android:text="@string/nexttxt"
                android:textColor="#FFFFFFFF" />        
        </LinearLayout></LinearLayout>
    layout1.xml
    <?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" >
       <ScrollView
        android:id="@+id/SCROLLER_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:fillViewport="true">  
        <TextView
            android:id="@+id/article"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:textSize="20sp"
            android:lineSpacingMultiplier="1.5"
             />
        </ScrollView>
    </LinearLayout>
    layout2.xml
    <?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" ><ListView
        android:id="@+id/question1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        android:divider="#00000000"
        android:fadingEdge="none" /> </LinearLayout>questiondetail.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >        <TextView
                android:id="@+id/questionnum_questiondetail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#030303"
                android:textSize="20sp" />        <TextView
                android:id="@+id/questiontxt_questiondetail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#030303"
                android:textSize="20sp" />
        </LinearLayout>               <RadioButton
                    android:id="@+id/answerA_questiondetail"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="" 
                    android:state_enabled="true"
                    android:textColor="#030303"
                    android:textSize="18sp"/>            <RadioButton
                    android:id="@+id/answerB_questiondetail"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="" 
                    android:state_enabled="true"
                    android:textColor="#030303"
                    android:textSize="18sp"/>            <RadioButton
                    android:id="@+id/answerC_questiondetail"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="" 
                    android:state_enabled="true"
                    android:textColor="#030303"
                    android:textSize="18sp"/>
                <RadioButton
                    android:id="@+id/answerD_questiondetail"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="" 
                    android:state_enabled="true"
                    android:textColor="#030303"
                    android:textSize="18sp"/>
            <TextView
                android:id="@+id/answerKey_questiondetail"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text=""     
                android:textSize="18sp"/>
    </LinearLayout>