1.實驗講義
實驗三 Android編程基礎——Activity(2課時)
一、實驗目的
1、學習android程序多個Activity的開發方法。
二、實驗內容
1、編寫一個程序,可在第一個Activity中輸入兩個整數,單擊“計算”按鈕後,在第二個Activity負責求和計算,並將結果顯示在第一個Activity中。
三、實驗步驟
1、建立一個android工程,修改main.xml文件並添加新的.xml文件,修改AndroidManifest.xml文件,編寫程序代碼,可在第一個Activity中輸入兩個整數,單擊“計算”按鈕後,在第二個Activity中負責求和計算,並將結果傳回並顯示在第一個Activity中。
四、考覈標準:
1、完成全部題目,設計合理,結果正確;評定爲A。
2、完成部分題目,設計比較合理,結果正確;根據實際情況評定爲B或C。
3、未獨立完成實驗要求;評定爲D。
2.代碼實現
2.1.文件結構
2.2.main界面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="請輸入第一個加數" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<EditText
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="請輸入第二個加數" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="相加" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="兩數相加和爲:" />
<TextView
android:id="@+id/resultsum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="null" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
2.3.scound界面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".SecoundActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="兩數相加的和爲:" />
<TextView
android:id="@+id/sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="關閉" />
</LinearLayout>
</RelativeLayout>
2.3.MainActivity
根據講義要求通過一箇中轉的Activity實現求和,通過bundle設置鍵值對進行傳值。
public class MainActivity extends AppCompatActivity {
//重寫 寫回方法
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0x11&&resultCode == 0x11){
Bundle bundle = data.getExtras();
Integer sum = bundle.getInt("sum");
TextView text = (TextView) findViewById(R.id.resultsum);
text.setText(sum.toString());
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲得按鈕
Button button = (Button) findViewById(R.id.button1);
//單擊事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String number1 = ((EditText) findViewById(R.id.textView)).getText().toString();
String number2 = ((EditText) findViewById(R.id.textView2)).getText().toString();
//判斷兩個空位都有值
if (!"".equals(number1) && !"".equals(number2)) {
//意圖對象
Intent intent = new Intent(MainActivity.this, SecoundActivity.class);
//創建Bundle綁定值
Bundle bundle = new Bundle();
//用hashmap保存值
bundle.putCharSequence("num1", number1);
bundle.putCharSequence("num2", number2);
intent.putExtras(bundle);
//執行意圖對象
startActivityForResult(intent, 0x11);
} else {
Toast.makeText(MainActivity.this, "請認真填寫", Toast.LENGTH_LONG).show();
}
}
});
}
}
2.4.SecoundActivity
通過intent = getIntent()獲得Intent對象,再通過intent.getExtras()獲得hash表。
並在關閉按鈕的方法中,進行回顯值的綁定。
public class SecoundActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secound);
final Intent intent = getIntent();
final Bundle bundle = intent.getExtras();
//字符串轉數字
Integer num1 = Integer.valueOf(bundle.getString("num1"));
Integer num2 = Integer.valueOf(bundle.getString("num2"));
//求和
final Integer sum = num1 + num2;
//獲取值的框,並以字符串顯示
TextView tv_sum = findViewById(R.id.sum);
tv_sum.setText(sum.toString());
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//綁定值
bundle.putInt("sum",sum);
intent.putExtras(bundle);
setResult(0x11,intent);
finish();
}
});
}
}
3.效果
1.輸入空會進行提示
2.正確輸入數字點擊相加的效果
3.點擊關閉後返回,將null改爲兩數之和
4.總結和改進
1.在輸入框傳值的時候,可以使用java的isDigit方法判斷是否爲數字,如果輸入錯誤,則返回錯誤信息。而不是進行空判斷。
2.Bundle的方法中有bundle.putInt進行整形的綁定也有put.CharSequence進行字符串的綁定。類似於HashMap.put(String,Integer/String)。
3.實驗三已完成,主要考察的應該是不同的activity之間的傳值,總體思想和後端頁面之間傳值類似。