MathQuiz-an Android Project

MathQuiz是我在香港大学学习Smart Phone这门课程的个人作业,一个用来进行数学测验的android app。
使用API Level 24 with minimum API Level 21,使用AVD (Nexus S with resolution 800X480).
这篇文章将归纳我做这个小项目过程中遇到的难点和收获。

目录结构

这个项目我使用了android studio,app是root目录,manifests是配置文件,java是编写的业务代码文件,res是资源文件。
android studio 文件目录结构

重要代码demo

页面跳转

1
2
3
Intent intent = new Intent(getBaseContext(), GradeActivity.class);
intent.putExtra("ansCorrect",ansCorrect);
startActivity(intent);

用户输入答案保留两位小数
这里我用了监听用户输入,当用户输入小数位数超过两位时,调用处理函数

1
2
3
4
5
6
7
8
9
10
11
boolean checkDecimal(String text){
Boolean ifFormat = false;
if (text.contains(".")) {
int index = text.indexOf(".");
if (index + 3 < text.length()) {
initPrompt("Warning","If your answers are not integers, please round them to 2 decimal places.");
ifFormat = true;
}
}
return ifFormat;
}

自定义图标提示框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void initPrompt(String txt, String msg) {
LayoutInflater inflater = getLayoutInflater();
View dialog = inflater.inflate(R.layout.prompt,(ViewGroup)findViewById(R.id.dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
TextView message = (TextView)dialog.findViewById(R.id.tv);
message.setText(msg);
builder.setTitle(txt);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setView(dialog);
builder.setIcon(R.drawable.logo);
builder.show();
}
1
2
3
4
5
6
7
8
9
10
11
12
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_margin="8dp"
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="textview show" />
</LinearLayout>

神奇的错误

在我调试代码的过程中控制台出现了这样的错误提示:

1
2
 10-22 14:00:11.310 1689-1689/com.android.inputmethod.latin E/RichInputConnection: Unable to connect to the editor to retrieve text.
10-22 14:00:11.310 1689-1689/com.android.inputmethod.latin D/RichInputConnection: Will try to retrieve text later.

这个错误会导致控制台无法打印信息,给我的调试带来很大的麻烦。我google了很久也没有找到合适的解决方案,最后我重建了一个项目,将相关代码复制才强硬解决了这个问题,如果还有好的方式,希望看到的人能联系我,谢谢了。

附录
项目成果 & github链接


MathQuiz-an Android Project
https://guoningyan.com/2017/11/17/MathQuiz-an-Android-Project/
作者
Ningyan Guo
发布于
2017年11月17日
许可协议