C50108
Search…
2019/0605/Java介面實作&結束+安卓工作室(整合Layout與Java)Intent意圖&Bundle包裹

前情提要 類別

多型呼叫處理
作業五 講解
下午 Inttent 資料傳遞 畫面整合

獨立活動 沒有資料交換 前景轉背景轉B畫面
相依活動 傳遞資料至下一個活動
修改前最好做備份
APP 很多 Activity 切換 利用 Intent
Values dimens.xml

這個就沒有傳遞參數

package com.example.intent01;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void button_Click(View v){
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
}
public void button01_Click(View v){
Intent intent = new Intent(this,ThreeActivity.class);
startActivity(intent);
}
}

package com.example.intent01;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
public void button2_Click(View v){
finish();
}
}

package com.example.intent01;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class ThreeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
}
public void button2_Click(View v){
finish();
}
}
自動在讀取完後顯示 要寫在 onCreate

package com.example.intent02;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class FActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_f);
convertTempture();
}
public void convertTempture(){
int c;
double f = 0.0;
Bundle bundle = this.getIntent().getExtras();
if(bundle != null){
c = Integer.parseInt(bundle.getString("TEMPC"));
f = (9.0 * c) / 5 + 32;
TextView o = findViewById(R.id.lblOutput);
o.setText("華氏溫度:"+Double.toString(f));
}
}
public void button2_Click(View V){
finish();
}
}

package com.example.intent02;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void button_Click(View V){
EditText txtC = findViewById(R.id.txtC);
Intent intent = new Intent(this,FActivity.class);
Bundle bundle = new Bundle();
bundle.putString("TEMPC",txtC.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}

Copy link
On this page
#Java
##繼承 介面 實作 / 抽象類別 override
意圖與程式生命週期
## alt + enter 快速引入package
Intent 意圖使用方式
bundle 包裹使用方式
下次預告