移动互联网实验三
- 1.实验讲义
- 2.代码实现
- 2.1.文件结构
- 2.2.main界面
- 2.3.scound界面
- 2.3.MainActivity
- 2.4.SecoundActivity
- 3.效果
- 4.总结和改进
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之间的传值,总体思想和后端页面之间传值类似。