1. 程式人生 > >1.5Android程式設計權威指南第一章程式碼

1.5Android程式設計權威指南第一章程式碼

在這裡插入圖片描述
activity_quiz.activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.study.android.geoquizactivity.QuizActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/question_text"/>

    <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>

strings.xml

<resources>
    <string name="app_name">GeoQuizActivity</string>
    <string name="question_text">Canberra is the captical of Australia</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>
</resources>

QuizActivity.java

public class QuizActivity extends AppCompatActivity {


    private Button mTrueButton;
    private Button mFalseButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        initView();
        initEvent();
    }

    private void initView() {
        //例項化控制元件
        mTrueButton = findViewById(R.id.true_button);
        mFalseButton = findViewById(R.id.false_button);
    }

    private void initEvent() {
        //設定匿名內部類監聽器
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
            }
        });

        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //定製Toast訊息,出現在螢幕上方
                Toast toast = Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP, 0, 0);
                toast.show();
            }
        });
    }
}

PS:
在AS中加m字首區別
在這裡插入圖片描述
Settings中加了m,設定成全域性變數後都會在最前面加上m字元
詳細設定步驟:
在這裡插入圖片描述

Demo下載地址:
https://download.csdn.net/download/weixin_43953649/10849702