制作一个简易的地理知识答题app,界面如下:
用户判断文中的地理知识是否正确,点击TRUE/FALSE按钮进行答题,在下方显示答题正确或不正确。
首先,先做出如图所示的布局,不含功能实现
(activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question_australia"
android:padding="24dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"
/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"
/>
</LinearLayout>
</LinearLayout>
这里用到了:
一个垂直的LinearLayout;
一个TextView显示地理题目
一个水平的LinearLayout包含两个按钮TRUE/FALSE
LinearLayout:
LinearLayout的android:layout_width属性有两个值
match_parent:视图与父视图大小相同。
wrap_content:视图将根据其显示内容自动调整大小(以前还有fill_parent等同于match_parent,现在已经废弃不用了)
第一个LinearLayout虽然是根元素,但是它也有父视图——Android提供该父视图来容纳应用的整个视图层结构。
android:orientation:设置布局方向,水平或者垂直
这里我们的根元素LinearLayout是垂直排列的,其中的组件按照在代码中出现的顺序,从上到下依次垂直排列。
第二个LinearLayout包含两个按钮,我们需要让两个按钮水平排列,所以这里android:orientation=“horizontal”
TextView:
TextView显示地理题目
android:text="@string/question_australia"显示要在文本框里显示的内容,@string是对字符串资源的引用,虽然可以强编码( android:text=“题目内容”),但是不推荐。引用字符串资源,我们要先在res->values->strings.xml中写入要引入的字符串
<resources>
<string name="app_name">GeoQuiz</string>
<string name="true_button">True</string>
<string name="false_button">False</string>
<string name="correct_toast">Correct!</string>
<string name="incorrect_toast">Incorrect!</string>
<string name="question_australia">Canberra is the capital of Australia.</string>
</resources>
Button:
Button用来点击TRUE/FALSE
android:id="@+id/true_button",这里的id主要是为接下来的功能实现作准备。
(MainActivity.java)
package com.example.geoquiz;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button mTrueButton;//正确按钮
private Button mFalseButton;//错误按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//引用true_button
mTrueButton =(Button)findViewById(R.id.true_button);
//设置监听器,这里是使用匿名内部类
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {//这里显示回答正确
Toast.makeText(this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
}
});
mFalseButton = (Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
}
});
}
}