前言
提示:部分資源來源網路,僅供學習,侵權即刪!
參考書目:《Android 編程權威指南》
本次將帶你開發本書第一個應用, 并借此學習一些Android基本概念以及構成應用的UI組件,馬上要開發的應用名叫GeoQuiz,它能給出一道道地理知識問題,用戶點擊TRUE或FALSE按鈕來回答螢屏上的問題,GeoQuiz即時作出反饋,下圖為用戶點擊TRUE按鈕的結果,

第一步:
第二步:

第三步:
因為本人的專案名稱涉及學號等隱私,故打碼,讀者在命名時隨意就行

第四步:
在對應位置輸入對應的代碼

package com.aliyun.appname;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFlaseButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(MainActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
//Does nothing yet,but soon!
}
});
mFlaseButton = (Button) findViewById(R.id.flase_button);
mFlaseButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(MainActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show();
//Does nothing yet,but soon!
}
});
}
}
<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:padding="24dp"
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/flase_button"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button" />
</LinearLayout>
</LinearLayout>
<resources>
<string name="app_name">appname</string>
<string name="question_text">Canberra is the capital 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>
第五步:運行

模擬手機上運行

下載到手機上運行

注:大致的操作流程就是如此,部分細節不懂的話讀者可以根據自身需要翻閱上一篇博客,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/229231.html
標籤:其他
