activity_main.xml
<?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"
android:padding="3dp"
android:background="#FFF6CF"
tools:context="shuati.made.tpp.shuati.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#9FFF"
android:text="中国工人阶级以独立的姿态登上政治舞台的是( C )"
android:textColor="#111"
android:id="@+id/tm"
/>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_height="50dp">
<Button
android:layout_width="150dp"
android:text="A选项"
android:background="#4FE1EB"
android:id="@+id/AS"
android:layout_height="match_parent" />
<View
android:layout_width="30dp"
android:layout_height="match_parent"/>
<Button
android:layout_width="150dp"
android:background="#4FE1EB"
android:text="B选项"
android:id="@+id/BS"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_height="50dp">
<Button
android:layout_width="150dp"
android:background="#4FE1EB"
android:id="@+id/CS"
android:text="C选项"
android:layout_height="match_parent" />
<View
android:layout_width="30dp"
android:layout_height="match_parent"/>
<Button
android:layout_width="150dp"
android:background="#4FE1EB"
android:text="D选项"
android:id="@+id/DS"
android:layout_height="match_parent" />
</LinearLayout>
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#9FFF"
android:layout_weight="0.5"
android:id="@+id/xx"
android:text="中国工人阶级以独立的姿态登上政治舞台的是( C )"
android:textColor="#111"
/>
<CheckBox
android:layout_width="match_parent"
android:text="随机出题"
android:id="@+id/sjct"
android:textColor="#E36"
android:layout_height="30dp" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:background="#FCEAED"
android:layout_height="40dp">
<TextView
android:layout_width="200dp"
android:text=""
android:id="@+id/db"
android:textColor="#F00"
android:textSize="20dp"
android:layout_height="30dp" />
<TextView
android:layout_width="50dp"
android:layout_gravity="center_vertical"
android:text="跳转:"
android:textColor="#000"
android:textSize="15dp"
android:layout_height="20dp" />
<EditText
android:inputType="number"
android:id="@+id/tzn"
android:layout_width="65dp"
android:background="#FFF"
android:text="0"
android:layout_height="30dp" />
<TextView
android:layout_width="60dp"
android:text="/0"
android:textColor="#000"
android:textSize="15dp"
android:id="@+id/geshu"
android:layout_marginRight="3dp"
android:layout_height="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="#F1b4ca"
android:layout_height="50dp">
<TextView
android:layout_width="100dp"
android:text="正确选项:"
android:textSize="20dp"
android:layout_height="30dp" />
<TextView
android:layout_width="150dp"
android:textColor="#6F3"
android:text="正确选项:"
android:textSize="30dp"
android:id="@+id/zq"
android:layout_height="40dp" />
<View
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<Button
android:layout_width="100dp"
android:text="查看答案"
android:id="@+id/ck"
android:textColor="#0A0"
android:background="#EEE"
android:layout_marginRight="3dp"
android:layout_height="40dp" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_weight="0.3"
android:layout_height="10dp">
<Button
android:layout_width="100dp"
android:text="提交"
android:textColor="#00B"
android:background="#B5c9d9"
android:id="@+id/sub"
android:layout_height="50dp" />
<Button
android:layout_marginLeft="5dp"
android:layout_width="100dp"
android:text="跳转"
android:id="@+id/tz"
android:background="#B5c9d9"
android:textColor="#0A0"
android:layout_height="50dp" />
<Button
android:layout_marginLeft="5dp"
android:layout_width="100dp"
android:text="下一题"
android:id="@+id/next"
android:background="#B5c9d9"
android:textColor="#8A0"
android:layout_height="50dp" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package shuati.made.tpp.shuati;
import android.app.Activity;
import android.graphics.Color;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.os.Handler;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.logging.LogRecord;
public class MainActivity extends Activity {
boolean you_sel[];
Timu timu;
Tm tm;
boolean is_suiji=false;
Button A,B,C,D,tijiao,ck,tz,next;
CheckBox sjct;
Button btns[];
EditText tzn;
TextView timu1,daan1,zhengque,db,geshu;
String debug;
Handler handler=new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what)
{
case 0:
for(int i=0;i<4;i++)
you_sel[i]=false;
geshu.setText("/"+timu.tm.size());
tm=timu.get_a_timu();
tzn.setText(""+(timu.pos+1));
timu1.setText(tm.timu);
daan1.setText(tm.xuanxiang);
db.setText("");
for(Button btn : btns)
{
btn.setTextColor(Color.BLACK);
}
zhengque.setText("");
break;
case 1:
//Toast.makeText(MainActivity.this,debug,Toast.LENGTH_SHORT).show();
db.setText(debug);
break;
case 2:
db.setTextColor(Color.GREEN);
zhengque.setText(tm.daan);
break;
case 3:
String h=tzn.getText().toString();
if( h==null || h.length()<=0)
h="0";
int pos=Integer.valueOf(h);
if(pos>timu.tm.size()||pos<1)
{
tzn.setText("1");
Toast.makeText(MainActivity.this,"超出范围!",Toast.LENGTH_SHORT).show();
return;
}
for(int i=0;i<4;i++)
you_sel[i]=false;
geshu.setText("/"+timu.tm.size());
tm=timu.tm.get(pos-1);
timu.pos=pos-1;
timu1.setText(tm.timu);
daan1.setText(tm.xuanxiang);
db.setText("");
for(Button btn : btns)
{
btn.setTextColor(Color.BLACK);
}
zhengque.setText("");
break;
case 5:
db.setTextColor(Color.RED);
break;
case 6:
String h1=tzn.getText().toString();
if( h1==null || h1.length()<=0)
h1="0";
int pos1=Integer.valueOf(h1);
pos1=pos1+1;
if(pos1>timu.tm.size()||pos1<1)
pos1=1;
tzn.setText(""+pos1);
for(int i=0;i<4;i++)
you_sel[i]=false;
geshu.setText("/"+timu.tm.size());
tm=timu.tm.get(pos1-1);
timu.pos=pos1-1;
timu1.setText(tm.timu);
daan1.setText(tm.xuanxiang);
db.setText("");
for(Button btn : btns)
{
btn.setTextColor(Color.BLACK);
}
zhengque.setText("");
break;
}
super.handleMessage(msg);
}
};
public void sendStr(String h)
{
debug=h;
handler.sendEmptyMessage(1);
}
public void sendMsg(int what)
{
switch (what)
{
case 0:
for(int i=0;i<4;i++)
you_sel[i]=false;
handler.sendEmptyMessage(0);
break;
}
}
public class Click implements View.OnClickListener{
int tag;
public Click(int tag)
{
this.tag=tag;
}
@Override
public void onClick(View v) {
if(tm==null)
{
debug="文件未加载!";
handler.sendEmptyMessage(1);
return;
}
if(tm.sel_num==1)
{
for(int i=0;i<4;i++)
{
Button btn=btns[i];
if(i==this.tag)
{
btn.setTextColor(Color.RED);
you_sel[this.tag]=true;
}
else
{
btn.setTextColor(Color.BLACK);
you_sel[i]=false;
}
}
}else{
you_sel[this.tag]=! you_sel[this.tag];
if(you_sel[this.tag])
{
Button btn=(Button)v;
btn.setTextColor(Color.RED);
}
else
{
Button btn=(Button)v;
btn.setTextColor(Color.BLACK);
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
you_sel=new boolean[4];
for(int i=0;i<4;i++)
you_sel[i]=false;
A=(Button)findViewById(R.id.AS);
B=(Button)findViewById(R.id.BS);
C=(Button)findViewById(R.id.CS);
D=(Button)findViewById(R.id.DS);
sjct=(CheckBox)findViewById(R.id.sjct);
sjct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
is_suiji=sjct.isChecked();
}
});
tijiao=(Button)findViewById(R.id.sub);
tz=(Button)findViewById(R.id.tz);
tz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.sendEmptyMessage(3);
}
});
next=(Button)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(is_suiji)
handler.sendEmptyMessage(0);
else
handler.sendEmptyMessage(6);
}
});
tzn=(EditText)findViewById(R.id.tzn);
ck=(Button)findViewById(R.id.ck);
ck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.sendEmptyMessage(2);
}
});
tijiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean ok=true;
for(int i=0;i<4;i++)
{
if(you_sel[i])
{
ok=false;
break;
}
}
if(ok)
{
sendStr("你还没选呢!");
return;
}
ok=true;
for(int i=0;i<4;i++)
{
if(you_sel[i]!=tm.bl[i])
{
ok=false;
break;
}
}
if(ok){
sendStr("你答对了!");
handler.sendEmptyMessage(2);
}
else
{
sendStr("回答错误!");
handler.sendEmptyMessage(5);
}
}
});
btns=new Button[4];
btns[0]=A;
btns[1]=B;
btns[2]=C;
btns[3]=D;
int i=0;
for(Button btn : btns)
{
btn.setOnClickListener(new Click(i++));
}
timu1=(TextView)findViewById(R.id.tm);
geshu=(TextView)findViewById(R.id.geshu);
daan1=(TextView)findViewById(R.id.xx);
db=(TextView)findViewById(R.id.db);
zhengque=(TextView)findViewById(R.id.zq);
zhengque.setText("");
timu=new Timu(getAssets(),this);
}
}
Tm.java
package shuati.made.tpp.shuati;
/**
* Created by cherry13 on 2024/4/25.
*/
public class Tm {
public int sel_num=1;
public String timu;
public String xuanxiang;
public boolean bl[];
public String daan;
public Tm(String t)
{
String t1[]=t.split("FFF");
this.timu=t1[0].replace("\\n","\n");
this.sel_num=Integer.valueOf(t1[1]);
this.bl=new boolean[4];
for(int i=0;i<4;i++)
this.bl[i]=false;
String h=t1[2];
for(char i:h.toCharArray())
{
switch (i)
{
case 'a':case 'A':
this.bl[0]=true;
break;
case 'b':case 'B':
this.bl[1]=true;
break;
case 'c':case 'C':
this.bl[2]=true;
break;
case 'd':case 'D':
this.bl[3]=true;
break;
}
}
this.daan=h;
this.xuanxiang=t1[3].replace("\\n","\n");
}
}
Timu.java
package shuati.made.tpp.shuati;
import android.content.res.AssetManager;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Created by cherry13 on 2024/4/25.
*/
public class Timu {
public List<Tm> tm;
public int pos=0;
public AssetManager AM;
public MainActivity maina;
Random rd=new Random(System.currentTimeMillis());
public Tm get_a_timu()
{
pos=Math.abs(rd.nextInt())%tm.size();
return tm.get(pos);
}
public Timu(AssetManager AM1,MainActivity maina1)
{
maina=maina1;
this.AM=AM1;
tm=new ArrayList<Tm>();
Thread th=new Thread(new Runnable() {
@Override
public void run() {
try
{
InputStreamReader FR=new InputStreamReader(AM.open("1.txt"));
BufferedReader BR=new BufferedReader(FR);
while(true)
{
String h=BR.readLine();
if(h==null || h.length()<=11)
break;
//maina.sendStr(h);
tm.add(new Tm(h));
}
FR.close();
maina.sendMsg(0);
}catch (IOException e){
}
}
});
th.start();
}
}
在Assets文件夹中加入1.txt作为题库
格式为
题目FFF正确选项个数FFF正确的选项FFF选项文本
例如:
五四运动爆发的直接原因是( )FFF1FFFBFFFA.俄国十月革命胜利的影响\nB.巴黎和会上中国外交的失败\nC.中国人民与封建军阀矛盾加剧\nD.中国无产阶级队伍的壮大