Android Debugger 初階使用
在這個文章中,用影片來說明 Android Studio Debugger 的使用方式。
上圖中使用者輸入一串的數字(用空白隔開),按下「計算」後,程式會幫忙算出平均值。 這是個簡單的例子,程式碼以及 layout 檔放在文章的最後,請大家參考。
上圖中使用者輸入一串的數字(用空白隔開),按下「計算」後,程式會幫忙算出平均值。 這是個簡單的例子,程式碼以及 layout 檔放在文章的最後,請大家參考。
詳細使用方式採用影片說明。
底下是MainActivity.java 的程式碼
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.justim.debugandroidapp; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
private String[] getScores() { | |
EditText edScores = (EditText) findViewById(R.id.edScores); | |
return edScores.getText().toString().split(" "); | |
} | |
private int[] strArray2IntArray(String[] arr) { | |
int[] buf = new int[arr.length]; | |
for(int i = 0; i < arr.length; i++) | |
buf[i] = Integer.parseInt(arr[i]); | |
return buf; | |
} | |
private double getAverage(int[] scores) { | |
double sum = 0.0; | |
for(int i = 0; i < scores.length; i++) { | |
sum += scores[i]; | |
} | |
return sum / scores.length; | |
} | |
public void calcAvg(View view) { | |
String[] str_scores = getScores(); | |
int[] scores = strArray2IntArray(str_scores); | |
double avg = getAverage(scores); | |
TextView tvAvg = (TextView)findViewById(R.id.tvAvg); | |
tvAvg.setText("平均為:" + String.valueOf(avg)); | |
} | |
} |
底下是 layout 檔的內容:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="com.example.justim.debugandroidapp.MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textAppearance="?android:attr/textAppearanceLarge" | |
android:text="分數群(用空白隔開)" | |
android:id="@+id/tvScores" | |
android:layout_alignParentTop="true" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" /> | |
<EditText | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/edScores" | |
android:layout_below="@+id/tvScores" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" /> | |
<Button | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:text="計算" | |
android:id="@+id/button" | |
android:layout_below="@+id/edScores" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" | |
android:onClick="calcAvg" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textAppearance="?android:attr/textAppearanceLarge" | |
android:text="平均" | |
android:id="@+id/tvAvg" | |
android:layout_below="@+id/button" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" /> | |
</RelativeLayout> |
留言
張貼留言