2017年3月28日作业----Activity的跳转及Activity之间的数据传递<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln...

 完成如图所示的任务:

a)在主屏幕输入自己的姓名,单击进入评估按钮,进入第二个界面,并将主屏幕输入的姓名传递给第二个界面;

b)在第二个界面进行问题回答;

c)第二个界面的回答结果返回第一个界面并显示。

创建布局文件activity_main.xml和activity_test.xml文件

activity_main的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.edu.niit.assess.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="@string/edit"
android:textAlignment="center"
android:maxLines="1"
android:maxLength="4"
android:layout_marginTop="200dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bt_test"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/bt_test"/>

<Button
android:id="@+id/bt_exit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/bt_exit"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal|top"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
做完以后调试代码的时候把横向和纵向搞错了,导致第一个EditText无法显示
activity_test的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:id="@+id/tv_qin"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal|bottom"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">

<Button
android:id="@+id/bt_YES"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/bt_yes" />

<Button
android:id="@+id/bt_NO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/bt_no" />

</LinearLayout>

</LinearLayout>
接下来在strings文件中编写代码,如下:
<resources>
<string name="app_name">Assess</string>
<string name="et_name">请输入姓名</string>
<string name="bt_test">进入评估</string>
<string name="bt_exit">退出</string>s
<string name="bt_yes">有</string>
<string name="bt_no">没有</string>
<string name="edit"></string>
</resources>
紧接着编写Java部分代码
MainActivity部分的代码如下:
package cn.edu.niit.assess;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private EditText et_name;
private TextView tv_result;
public static MainActivity mainActivity = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name=(EditText) findViewById(R.id.et_name);
tv_result=(TextView) findViewById(R.id.tv_result);
mainActivity =this;

try {
TestActivity.testActivity.finish();
Intent intent = getIntent();
String result = intent.getStringExtra("result");
String name=intent.getStringExtra("name");
et_name.setText(name);
tv_result.setText(result);
}catch (Exception e){

}
}

public void onClick(View view){
switch (view.getId()){
case R.id.bt_test:
String name = et_name.getText().toString();
Intent intent = new Intent(MainActivity.this,TestActivity.class);
Bundle bundle = new Bundle();
bundle.putString("qua",name);
intent.putExtras(bundle);
startActivity(intent);
break;
case R.id.bt_exit:
finish();
TestActivity.testActivity.finish();
break;
}
}
}
TestActivity部分的代码如下:
package cn.edu.niit.assess;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


public class TestActivity extends AppCompatActivity {
private TextView tv_qin;
private String name;
public static TestActivity testActivity = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
tv_qin = (TextView) findViewById(R.id.tv_qin);

testActivity =this;
MainActivity.mainActivity.finish();
Bundle bundle = getIntent().getExtras();
name = bundle.getString("qin");
tv_qin.setText(name+"同学:学习Android有没有信心?");
}
public void onClick(View view){

Intent intent;
intent = new Intent(TestActivity.this,MainActivity.class);
switch (view.getId()){
case R.id.bt_YES:
intent.putExtra("name",name);
intent.putExtra("result","评估内容返回为:有");
startActivity(intent);
finish();
break;
case R.id.bt_NO:
intent.putExtra("name",name);
intent.putExtra("result","评估内容返回为:没有");
startActivity(intent);
finish();
break;
}
}
}
最终如图所示:

 

转载于:https://www.cnblogs.com/feiyuning/p/6729272.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值