Milliondollars智力问答游戏,主要的技术要点(一)、读取题库数据xml文件(二)、如何动态的生成题目和选项。这里做个总结,供以后参考。
(一)、读取题库数据xml文件
将assets/topics.xml文件读取
topics.xml格式:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<question topic="《全唐诗》是哪个时期的人编辑的?">
<a>清朝</a>
<b>唐朝</b>
<c>明朝</c>
<d>元朝</d>
<answer>清朝</answer>
</question>
<question topic="荷兰是国花是?">
<a>荷花</a>
<b>玫瑰</b>
<c>风信子</c>
<d>郁金香</d>
<answer>郁金香</answer>
</question>
<question topic="乐山大佛历经多少年方才建成?">
<a>90</a>
<b>80</b>
<c>70</c>
<d>60</d>
<answer>90</answer>
</question>
<question topic="人的自我意识是从什么时候产生的?">
<a>2岁左右</a>
<b>1岁左右</b>
<c>3岁左右</c>
<d>4岁左右</d>
<answer>1岁左右</answer>
</question>
</book>
读取代码:
/**
* 从xml中读取所有的题目
*/
public void readTopicXml() {
DocumentBuilderFactory docBuilderFactory = null;
DocumentBuilder docBuilder = null;
Document doc = null;
try {
docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilder = docBuilderFactory.newDocumentBuilder();
// xml file 放到 assets目录中的
doc = docBuilder.parse(this.getResources().getAssets()
.open("topics.xml"));
// root element
Element root = doc.getDocumentElement();
// 获取所有的题目
NodeList nodeList = root.getElementsByTagName("question");
topics = new ArrayList<Topic>();
int nodelength = nodeList.getLength();
for (int a = 0; a < nodelength; a++) {
Node nd = nodeList.item(a);
Topic tp = new Topic();
tp.topicid = a;
// topics
tp.topic = nd.getAttributes().item(0).getNodeValue();
// items and answer
NodeList nl = nd.getChildNodes();
int childCount = nl.getLength();
for (int b = 0; b < childCount; b++) {
Node ndchild = nl.item(b);
if (ndchild instanceof Element) {
if (ndchild.getNodeName().equals("a")) {
tp.a = ndchild.getTextContent();
}
if (ndchild.getNodeName().equals("b")) {
tp.b = ndchild.getTextContent();
}
if (ndchild.getNodeName().equals("c")) {
tp.c = ndchild.getTextContent();
}
if (ndchild.getNodeName().equals("d")) {
tp.d = ndchild.getTextContent();
}
if (ndchild.getNodeName().equals("answer")) {
tp.answer = ndchild.getTextContent();
}
}
}
topics.add(tp);
}
} catch (Exception e) {
Log.e("err", e.getMessage());
Log.e("err", e.getStackTrace().toString());
} finally {
doc = null;
docBuilder = null;
docBuilderFactory = null;
Date d = new Date();
Log.i("info", d.toLocaleString()
+ ":MillionaireActivity.readTopicXml complete");
}
}
(二)、如何动态的生成题目和选项
/**
* 动态设置页面
*
* @param topics
*/
public void setLayout(List<Topic> topics) {
try {
int count = topics.size();
Random r = new Random();
int index = r.nextInt(count);
while (al.indexOf(index) != -1) {
index = r.nextInt(count);
}
al.add(index);// 已选择的题目
// scroll view
ScrollView sv = new ScrollView(this);
RelativeLayout.LayoutParams svp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
sv.setLayoutParams(svp);
setContentView(sv);
// relative layout
RelativeLayout svlayout = new RelativeLayout(this);
svlayout.setLayoutParams(svp);
sv.addView(svlayout);
tcRadom = topics.get(index);
// TextView
// 当前 得分
RelativeLayout.LayoutParams tp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tp.topMargin = 8;
tp.leftMargin = 8;
tp.rightMargin = 8;
TextView tv = new TextView(this);
tv.setId(USER_ID + 1);
tv.setText("当前得分:" + total + "美元");
// tv.setTextColor(Color.RED);
tv.setLayoutParams(tp);
svlayout.addView(tv);
// 题目
RelativeLayout.LayoutParams tpTopic = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tpTopic.topMargin = 8;
tpTopic.leftMargin = 8;
tpTopic.rightMargin = 8;
tpTopic.addRule(RelativeLayout.BELOW, USER_ID + 1);
TextView tvTopic = new TextView(this);
tvTopic.setId(USER_ID + 2);
tvTopic.setText(tcRadom.topic);
// tvTopic.setTextColor(Color.MAGENTA);
tvTopic.setLayoutParams(tpTopic);
svlayout.addView(tvTopic);
// RadioGroup 选项
items.clear();// 选择的题目对应的选项先清空
alChoseItems.clear();// 已经随机选出的选项先清空
items.add(tcRadom.a);
items.add(tcRadom.b);
items.add(tcRadom.c);
items.add(tcRadom.d);
RelativeLayout.LayoutParams rp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rp.topMargin = 8;
rp.leftMargin = 8;
rp.rightMargin = 8;
rp.addRule(RelativeLayout.BELOW, USER_ID + 2);
RadioGroup rg = new RadioGroup(this);
rg.setId(USER_ID + 3);
Random ra = new Random();
// A选项随机抽取
int itemAIndex = ra.nextInt(4);
while (alChoseItems.indexOf(itemAIndex) != -1) {
itemAIndex = ra.nextInt(4);
}
alChoseItems.add(itemAIndex);
RadioButton rba = new RadioButton(this);
rba.setText("A." + items.get(itemAIndex));
// B选项随机抽取
int itemBIndex = ra.nextInt(4);
while (alChoseItems.indexOf(itemBIndex) != -1) {
itemBIndex = ra.nextInt(4);
}
alChoseItems.add(itemBIndex);
RadioButton rbb = new RadioButton(this);
rbb.setText("B." + items.get(itemBIndex));
// C选项随机抽取
int itemCIndex = ra.nextInt(4);
while (alChoseItems.indexOf(itemCIndex) != -1) {
itemCIndex = ra.nextInt(4);
}
alChoseItems.add(itemCIndex);
RadioButton rbc = new RadioButton(this);
rbc.setText("C." + items.get(itemCIndex));
// D选项随机抽取
int itemDIndex = ra.nextInt(4);
while (alChoseItems.indexOf(itemDIndex) != -1) {
itemDIndex = ra.nextInt(4);
}
alChoseItems.add(itemDIndex);
RadioButton rbd = new RadioButton(this);
rbd.setText("D." + items.get(itemDIndex));
rg.addView(rba);
rg.addView(rbb);
rg.addView(rbc);
rg.addView(rbd);
rg.setLayoutParams(rp);
svlayout.addView(rg);
// Button 确定答题
RelativeLayout.LayoutParams bp = new RelativeLayout.LayoutParams(
150, RelativeLayout.LayoutParams.WRAP_CONTENT);
bp.topMargin = 8;
bp.leftMargin = 8;
bp.rightMargin = 8;
bp.addRule(RelativeLayout.BELOW, USER_ID + 3);
Button btn = new Button(this);
btn.setId(USER_ID + 4);
btn.setText("确 定");
btn.setLayoutParams(bp);
btn.setOnClickListener(this);
svlayout.addView(btn);
// TextView 是否回答正确提示
RelativeLayout.LayoutParams tpTip = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tpTip.topMargin = 8;
tpTip.leftMargin = 8;
tpTip.rightMargin = 8;
tpTip.addRule(RelativeLayout.BELOW, USER_ID + 4);
TextView tvTip = new TextView(this);
tvTip.setId(USER_ID + 5);
tvTip.setLayoutParams(tpTip);
svlayout.addView(tvTip);
} catch (Exception ex) {
Date d = new Date();
Log.i("err", d.toLocaleString() + ":MillionaireActivity.setLayout error,"
+ ex.getStackTrace());
}
Date d = new Date();
Log.i("info", d.toLocaleString() + ":MillionaireActivity.setLayout complete");