Android Studio 创建项目
??启动 Android Studio 进入到下图所示界面。如果选哟创建新的项目,选择 Create New Project,如果是打开现有项目,选择 Open an Existing Project。

??这里演示一下创建新项目的流程。选择 Empty Activity 点击 Next。
??这里选中 Empty Activity 是因为这个选项会默认帮我们创建好一个 Activity 的模板,作为初次接触 Android 的童鞋,选择 Empty Activity 会比较容易。而 No Activity 需要我们手动创建,至于如何手动创建 Activity 之后在 Activity 部分会讲到的。

??在接下来的页面中,修改好项目名称、包名、保存路径、使用的语言(默认是 Kotlin,需要修改为 Java),点击 Finish 即可。然后就会进入到 Android Studio 的编辑界面,此时需要等待比较长的一段加载时间,加载完成之后,即可见到下面的界面。

首个 Android 项目
??res/layout/activity_main.xml 文件是布局文件,在这里面的控件会显示在 Activity 页面中。

MainActivity 类的代码:
package com.gm.audio;
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 = null;
private Button mFalseButton = null;
private Button mNextButton = null;
private TextView mTextView = null;
private int mCurrentIndex = 0;
private QuestionModel[] mQuestionList = new QuestionModel[]{
new QuestionModel(R.string.q1, true),
new QuestionModel(R.string.q2, false),
new QuestionModel(R.string.q3, true),
new QuestionModel(R.string.q4, true),
new QuestionModel(R.string.q5, false),
new QuestionModel(R.string.q6, true),
new QuestionModel(R.string.q7, true)
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTrueButton = (Button) findViewById(R.id.true_button);
mFalseButton = (Button) findViewById(R.id.false_button);
mNextButton = (Button) findViewById(R.id.next_button);
mTextView = (TextView) findViewById(R.id.textView);
mTextView.setText(mQuestionList[0].getQuestion());
mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = ++mCurrentIndex%mQuestionList.length;
mTextView.setText(mQuestionList[mCurrentIndex].getQuestion());
}
});
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(MainActivity.this, R.string.correct, Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 100);
toast.show();
}
});
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(MainActivity.this, R.string.incorrect, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0, 0);
toast.show();
}
});
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = ++mCurrentIndex % 7;
mTextView.setText(mQuestionList[mCurrentIndex].getQuestion());
}
});
}
}
QuestionMode 类的代码:
package com.gm.audio;
public class QuestionModel {
private int mQuestion;
private boolean mAnswer;
public QuestionModel(int mQuestion, boolean mAnswer){
this.mAnswer = mAnswer;
this.mQuestion = mQuestion;
}
public int getQuestion() {
return mQuestion;
}
public boolean isAnswer() {
return mAnswer;
}
}
activity_main.xml
<?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=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:textSize="20dp" />
<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:layout_marginRight="10dp"
android:text="@string/app_true" />
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_false" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/next_button"
android:text="@string/next_btn"/>
</LinearLayout>
|