上偏加入字串空的
String all ="";
這裡多了餐選的,飲料選的,全部選的
String mealString,drinkString,all ="";
現在長這樣~
package com.huang.myapplication8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//宣告變數
EditText uName,uPhone,uNum;
TextView uPrice;
RadioGroup uMeal,uDrink;
RadioButton m1,m2,m3,m4,d1,d2,d3;
CheckBox memo1,memo2,memo3;
String mealString,drinkString,all ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
}
}
開始做初始化-findViewById
package com.huang.myapplication8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//宣告變數
EditText uName,uPhone,uNum;
TextView uPrice;
RadioGroup uMeal,uDrink;
RadioButton m1,m2,m3,m4,d1,d2,d3;
CheckBox memo1,memo2,memo3;
String mealString,drinkString,all ="";
//初始化
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uName =findViewById(R.id.uName);
uPhone =findViewById(R.id.uPhone);
uNum =findViewById(R.id.uNum);
uPrice =findViewById(R.id.uPrice);
//uMeal
}
public void onClick(View view) {
}
}
//uMealt初始化
uMeal=findViewById(R.id.uMeal);
加工能-偵測是否有改變setOnCheckedChangeListener-()裡面是物件
新增RadioGroup專用-單選
這裡看看-(RadioGroup radioGroup, int i)改成(RadioGroup group, int checkedId)會怎樣
//uMeal
uMeal=findViewById(R.id.uMeal);
uMeal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb =findViewById(checkedId);
mealString = rb.getText().toString();
Toast.makeText(MainActivity.this,mealString,Toast.LENGTH_SHORT).show();
rb在這裡是準備操作的臨時變數-
再來看mealString是點到誰-->然後轉成字串
用toast測試.最後註解掉:
package com.huang.myapplication8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//宣告變數
EditText uName,uPhone,uNum;
TextView uPrice;
RadioGroup uMeal,uDrink;
RadioButton m1,m2,m3,m4,d1,d2,d3;
CheckBox memo1,memo2,memo3;
String mealString,drinkString,all ="";
//初始化
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uName =findViewById(R.id.uName);
uPhone =findViewById(R.id.uPhone);
uNum =findViewById(R.id.uNum);
uPrice =findViewById(R.id.uPrice);
//uMeal
uMeal=findViewById(R.id.uMeal);
uMeal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb =findViewById(checkedId);
mealString = rb.getText().toString();
Toast.makeText(MainActivity.this,mealString,Toast.LENGTH_SHORT).show();
}
});
}
public void onClick(View view) {
}
}
toast要用手機測試:
這裡來寫uDrink要注意}
//uDrink
uDrink=findViewById(R.id.uDrink);
uDrink.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb =findViewById(checkedId);
drinkString = rb.getText().toString();
Toast.makeText(MainActivity.this,mealString,Toast.LENGTH_SHORT).show();
}
});
再來寫checkBox-初始化
package com.huang.myapplication8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//宣告變數
EditText uName,uPhone,uNum;
TextView uPrice;
RadioGroup uMeal,uDrink;
RadioButton m1,m2,m3,m4,d1,d2,d3;
CheckBox memo1,memo2,memo3;
String mealString,drinkString,all ="";
//初始化
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uName =findViewById(R.id.uName);
uPhone =findViewById(R.id.uPhone);
uNum =findViewById(R.id.uNum);
uPrice =findViewById(R.id.uPrice);
//uMeal
uMeal=findViewById(R.id.uMeal);
uMeal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb =findViewById(checkedId);
mealString = rb.getText().toString();
Toast.makeText(MainActivity.this,mealString,Toast.LENGTH_SHORT).show();
}
});
//uDrink
uDrink=findViewById(R.id.uDrink);
uDrink.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb =findViewById(checkedId);
drinkString = rb.getText().toString();
Toast.makeText(MainActivity.this,mealString,Toast.LENGTH_SHORT).show();
}
});
//checkBox
memo1 =findViewById(R.id.memo1);
memo2 =findViewById(R.id.memo2);
memo3 =findViewById(R.id.memo3);
}
public void onClick(View view) {
}
}
利用按鈕整合起來-收集資料並送到下一頁-一個一個字串string收集到下一頁
1-撰寫Andorid的時候,有時程式的功能會需要取得Button、TextView、EditText等等元件的內容,一般會認為getText()這個方法所回傳的類型是String,但是情況卻不是這樣,需要加上toString()的方法強制轉型為String
2-num數字欄位計算.但是如果是要字串轉數字就要用Integer.parseInt
public void onClick(View view) {
//文字欄位的收集
String name = uName.getText().toString();
String phone = uPhone.getText().toString();
int num = Integer.parseInt(uNum.getText().toString());
}
再來是checkBox-用陣列選取一下
用for each的寫法
https://lakesd6531.pixnet.net/blog/post/342603565-java-for-each%E8%BF%B4%E5%9C%88%E7%9A%84%E7%94%A8%E6%B3%95
i依序取出R.id.memo1,R.id.memo2,R.id.memo3
字串空的=全部選的
String all ="";
package com.huang.myapplication8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//宣告變數
EditText uName,uPhone,uNum;
TextView uPrice;
RadioGroup uMeal,uDrink;
RadioButton m1,m2,m3,m4,d1,d2,d3;
CheckBox memo1,memo2,memo3;
String mealString,drinkString,all ="";
//初始化
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uName =findViewById(R.id.uName);
uPhone =findViewById(R.id.uPhone);
uNum =findViewById(R.id.uNum);
uPrice =findViewById(R.id.uPrice);
//uMeal
uMeal=findViewById(R.id.uMeal);
uMeal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb =findViewById(checkedId);
mealString = rb.getText().toString();
Toast.makeText(MainActivity.this,mealString,Toast.LENGTH_SHORT).show();
}
});
//uDrink
uDrink=findViewById(R.id.uDrink);
uDrink.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb =findViewById(checkedId);
drinkString = rb.getText().toString();
Toast.makeText(MainActivity.this,mealString,Toast.LENGTH_SHORT).show();
}
});
//checkBox
memo1 =findViewById(R.id.memo1);
memo2 =findViewById(R.id.memo2);
memo3 =findViewById(R.id.memo3);
}
//收集資料並送到下一頁
public void onClick(View view) {
//文字欄位的收集
String name = uName.getText().toString();
String phone = uPhone.getText().toString();
int num = Integer.parseInt(uNum.getText().toString());
//checkBox
int[] cbs ={R.id.memo1,R.id.memo2,R.id.memo3};
for(int i:cbs){
CheckBox cc =findViewById(i);
if(cc.isChecked()){
all += cc.getText().toString()+"\t";
}
}
}
}
xml+java檔就是activity就是活動畫面
再來是送到第2頁:Intent意圖 from->page2
按app-->java--mainActivity第2頁的建立開始很像開新檔:要V--layout file
等他初始化完成-java檔和layout檔案個多一個
看到manifests-設定權限要求(打開相機之類的)-活動畫面的清冊
裡面顯示一個APP可以看到多少頁面
<intent-filter的功能是:
1-指定-頁面
2-條件action
MAIN是指主要開始
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
繼續從 送到第2頁
//送到第2頁
Intent it = new Intent();//送貨員
it.setClass(MainActivity.this,Page2.class);//送到哪裡去
startActivity(it);//出發了
目前手動回到上一頁~