Date: 20181013
Version: 1
主要是用來記錄學習Android Studio的過程紀錄。
1. build.gradle
##
apply plugin: 'com.android.application'
android {
compileSdkVersion 28 <= 這邊要注意,故SDK須要API 28
buildToolsVersion "28.0.3" <= 這邊要注意
defaultConfig {
applicationId "com.blogspot.iamamu.first"
minSdkVersion 22 <= 這邊要注意,故SDK須要API 22
targetSdkVersion 28 <= 這邊要注意
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0' <= 這邊要注意
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
2. ADM
使用API 22 x86 Google API 即可
3. 翻譯文字
使用locale zh TW即可,@strings/"字串"
4. 熱鍵
Ctrl + Alt + L:重新排版
Ctrl + F1:more 說明
Alt + Enter:檢查錯誤,並提供修正參考
5. 目前在寫按鈕按下後,呈顯會加一,寫完當下,Android Studio會先自檢,若有錯誤,則會跳出紅色折線,目前寫完是沒有出現這樣的狀況,但不知為何,每次運用模擬器時,都會當掉,下次我會用公司的電腦試試看,看到底哪裡出現了問題。
/*MainActivity.java
package com.wood.second;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//宣告
private Button button = findViewById(R.id.button);
private TextView textView = findViewById(R.id.textView);
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
count++;
textView.setText(count);
}
}
*/
6. 在公司試了半天,終於找到原因了,也順利run起來了,要注意的點是
- 要先宣告,但不先不賦予值。
- 再來onclick內要有確認ID的動作。
/*
package com.wood.second;
import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnplus, btnminus, btnreset, btnmulti, btndiv;
private TextView txv;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnplus = findViewById(R.id.button);
btnminus = findViewById(R.id.button2);
btnreset = findViewById(R.id.button3);
btnmulti = findViewById(R.id.button4);
btndiv = findViewById(R.id.button5);
//Button btnplus = findViewById(R.id.button);
//Button btnminus = findViewById(R.id.button2);
txv = findViewById(R.id.textView);
//count = 0;
btnplus.setOnClickListener(this);
btnminus.setOnClickListener(this);
btnreset.setOnClickListener(this);
btnmulti.setOnClickListener(this);
btndiv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
count++;
break;
case R.id.button2:
count--;
break;
case R.id.button3:
count = 0;
break;
case R.id.button4:
count *= count;
break;
case R.id.button5:
count /= count;
break;
default:
break;
}
txv.setText(Integer.toString(count));
}
}
*/
7. 如何解決setText會出現建議事項,提供參考網站,【Android寻坑之路】解决TextView.setText提示Do not concatenate text displayed with setText. Use resource string with placeholders.问题,文章中大約出現了4種轉字串的作法如下:
- @SuppressLint("SetTextI18n"); txv.setText(Integer.toString(count));
- txv.setText(String.valueOf(count)); <=詢問寫java的人,比較常用
- StringBuilder a = new StringBuilder(); txv.setText(a.append(count)); <=詢問寫java的人,比較常用
- android:text="@string/number" ==> <string name="number">%1$d</string> ==> txv.setText(String.format(getResources().getString(R.string.number),count));
8. 解決了按鈕的問題後,接下來要試試看下拉式選單,參考"GiveMePasS'S Android惡補筆記-如何使用Spinner(下拉式選單)"這一篇文章。再來是整體設計,會有一個按鈕,按下後會隨機取樣(1-10),這個數字就是下拉式選單中會有多少選項。
也順便學了java的相關函式:
- Math.random()
- ArrayList<> = new ArrayList<>();
- ArrayAdapter<> = new ArrayAdapter<>();
- ArrayList.add()
- ArrayList.clear()
- ArrayList.get()
整體來說,還不錯,再接再勵往專業邁進。
9. 下拉式選單完成後,我接著挑戰日期和時間,也一樣上網爬了文,DatePicker和TimePicker,這兩篇對我幫助很大,後來也自行修改到可以記錄上一筆選擇的項目,不會每次點進去都是同一時間,也學了許多java函式:
- Calendar = Calendar.getInstance()
- Calendar.set()
- Calendar.get()
- Calendar.YEAR
- Calendar.MONTH
- Calendar.DAY_OF_MONTH
- Calendar.DAY_OF_WEEK
- Calendar.HOUR_OF_DAY
- Calendar.MINUTE
- String.format("%d...", ...)
- Key-Value Pairs:
- Map<> = new HashMap<>()
- HashMap.put()
- HashMap.get()
/*
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.content.Context;
import android.renderscript.Int3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements OnClickListener, AdapterView.OnItemSelectedListener {
private Button btnplus, btnminus, btnreset, btnmulti, btndiv, btnrand, btninput, btndate, btntime;
private TextView txv, txv2, txv3;
private Spinner spin;
private int count = 0;
ArrayList<String> randomList = new ArrayList<>();
private EditText editxt;
private Map<Integer, String> dayOfWeek = new HashMap<>();
final Calendar time = Calendar.getInstance();
private int myYear = time.get(Calendar.YEAR), myMonth = time.get(Calendar.MONTH), myDay = time.get(Calendar.DAY_OF_MONTH), myHour = time.get(Calendar.HOUR_OF_DAY), myMin = time.get(Calendar.MINUTE);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnplus = findViewById(R.id.button);
btnminus = findViewById(R.id.button2);
btnreset = findViewById(R.id.button3);
btnmulti = findViewById(R.id.button4);
btndiv = findViewById(R.id.button5);
btnrand = findViewById(R.id.button6);
btninput = findViewById(R.id.button7);
btndate = findViewById(R.id.button8);
btntime = findViewById(R.id.button9);
txv = findViewById(R.id.textView);
txv2 = findViewById(R.id.textView2);
txv3 = findViewById(R.id.textView3);
txv.setText(String.valueOf(count));
spin = findViewById(R.id.spinner);
editxt = findViewById(R.id.editText);
btnplus.setOnClickListener(this);
btnminus.setOnClickListener(this);
btnreset.setOnClickListener(this);
btnmulti.setOnClickListener(this);
btndiv.setOnClickListener(this);
btnrand.setOnClickListener(this);
spin.setOnItemSelectedListener(this);
btninput.setOnClickListener(this);
btndate.setOnClickListener(this);
btntime.setOnClickListener(this);
dayOfWeek.put(1, "日");
dayOfWeek.put(2, "一");
dayOfWeek.put(3, "二");
dayOfWeek.put(4, "三");
dayOfWeek.put(5, "四");
dayOfWeek.put(6, "五");
dayOfWeek.put(7, "六");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
count++;
break;
case R.id.button2:
count--;
break;
case R.id.button3:
count = 0;
break;
case R.id.button4:
count *= count;
break;
case R.id.button5:
count /= count;
break;
case R.id.button7:
count = (editxt.getText().length() == 0) ? 0 : Integer.valueOf(editxt.getText().toString());
//count = Integer.valueOf(editxt.getText().toString());
//txv.setText(editxt.getText().toString());
break;
case R.id.button6:
int rand = (int) (Math.random() * 10 + 1);
randomList.clear();
for (int i = 1; i <= rand; i++) {
randomList.add(String.valueOf(i));
}
ArrayAdapter<String> randomAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, randomList);
spin.setAdapter(randomAdapter);
break;
case R.id.button8:
//final Calendar time = Calendar.getInstance();
//myYear = time.get(Calendar.YEAR);
//myMonth = time.get(Calendar.MONTH);
//myDay = time.get(Calendar.DAY_OF_MONTH);
//myDate = time.get(Calendar.DAY_OF_WEEK);
//txv2.setText(String.valueOf(myYear) + String.valueOf(myMonth + 1) + String.valueOf(myDay) + String.valueOf(myDate));
new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
time.set(year, month, dayOfMonth);
myYear = year;
myMonth = month;
myDay = dayOfMonth;
txv2.setText(String.format("%d-%d-%d (%s)", year, month + 1, dayOfMonth, dayOfWeek.get(time.get(Calendar.DAY_OF_WEEK))));
//txv2.setText(String.valueOf(year) + String.valueOf(month + 1) + String.valueOf(dayOfMonth) + " (" + dayOfWeek.get(time.get(Calendar.DAY_OF_WEEK)) + ")");
}
}, myYear, myMonth, myDay).show();
break;
case R.id.button9:
//myHour = time.get(Calendar.HOUR_OF_DAY);
//myMin = time.get(Calendar.MINUTE);
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.set(myYear,myMonth,myDay,hourOfDay,minute);
myHour = hourOfDay;
myMin = minute;
txv3.setText(String.format("%d:%d", hourOfDay, minute));
}
}, myHour, myMin, true).show();
break;
default:
break;
}
//txv.setText(Integer.toString(count));
txv.setText(String.valueOf(count));
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
count = Integer.valueOf(randomList.get(position));
txv.setText(String.valueOf(count));
//Toast.makeText(this, "The number is: " + randomList.get(position), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
*/
沒有留言:
張貼留言