Android開發初體驗
前言
我們已經學習過Android開發的入門級程式 現在我們近一步學習Android,來跟著我學習吧!!!
步驟
一. 打開Android studio,新建一個工程,



二.進行布局,
1.首先我們需要以下組件
一個垂直LinearLayout組件
一個TextView組件
一個水平LinearLayout組件
兩個Button組件
2.在XML檔案中定義組件,把以下代碼刪掉,重新設定,
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3.更改為以下代碼
<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="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="true_button" />
<Button
android:id="@+id/flase_button"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:text="/false_button" />
</LinearLayout>
</LinearLayout>
4.這樣就完成布局了,回傳手機界面就可以看到設定好的布局

三.創建字串資源,在string.xml中添加,

五.打開java的檔案,近一步添加成員變數,
六.繼續添加代碼,參考組件,
在這串代碼下,添加代碼

代碼如下:
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!
}
});
七.再次添加一個組件button,代碼如下:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT >" />
在這個地方添加

八.創建新類
點擊java,右鍵new一個java class,命名為Question,點擊ok,

九.在這個檔案里添加代碼,代碼如下:
private int mTextResId;
private boolean mAnswerTrue;
public Question(int mTextResId,boolean mAnswerTrue){
mTextResId=mTextResId;
mAnswerTrue=mAnswerTrue;
然后配置Android studio識別成員變數的前綴

十.生成getter方法與setter方法,在新建java檔案下,代碼如下
public int getTextResId() {
return mTextResId;
}
public void setTextResId(int textResId){
mTextResId=textResId;
}
public boolean isAnswerTrue(){
return mAnswerTrue;
}
public void setAnswerTrue(boolean answerTrue){
mAnswerTrue=answerTrue;
}

十一.連接模擬器

十二.連接手機,實作真機,

總結
希望我們能一起進步,感受編程的快樂,有錯誤請指出,謝謝~
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/229237.html
標籤:其他
