跳转
一、XML代码
<?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_tiaozhuan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.messi.tiaozhuanapplication.Tiaozhuan">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/xm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/xm"
android:gravity="center"
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/btn_getin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/btn_in"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_getout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/btn_out"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/jieguo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/jieguo"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
二、Activity代码
package com.example.messi.tiaozhuanapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Tiaozhuan extends AppCompatActivity {
private EditText xm;
private Button btn_getin;
private Button btn_getout;
private TextView jieguo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tiaozhuan);
xm = (EditText)findViewById(R.id.xm);
btn_getin = (Button)findViewById(R.id.btn_getin);
btn_getout = (Button)findViewById(R.id.btn_getout);
jieguo = (TextView)findViewById(R.id.jieguo);
String data="";
Intent intent=getIntent();
String jg=intent.getStringExtra("result");
TextView tvresult=(TextView)findViewById(R.id.jieguo);
data = jg;
tvresult.setText(data);
}
public void onClick(View view){
switch (view.getId()){
case R.id.btn_getin:
question();
break;
case R.id.btn_getout:
finish();
break;
}
}
private void question() {
Intent intent = new Intent();
intent.setClass(Tiaozhuan.this,pingguActivity.class);
String text =xm.getText().toString();
intent.putExtra("name",text+"你是不是对安卓课很有兴趣?");
startActivity(intent);
}
}
三、跳转后的XML代码
<?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_pinggu"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.messi.tiaozhuanapplication.pingguActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/wenti"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18sp"
android:textAlignment="center"
android:layout_marginTop="200dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_yes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/btn_yes"
android:textSize="30dp"
android:onClick="onClick"/>
<Button
android:id="@+id/btn_no"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/btn_no"
android:textSize="30dp"
android:onClick="onClick" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
package com.example.messi.tiaozhuanapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class pingguActivity extends AppCompatActivity {
private TextView wenti;
private Button btn_yes;
private Button btn_no;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pinggu);
wenti = (TextView)findViewById(R.id.wenti);
btn_yes = (Button)findViewById(R.id.btn_yes);
btn_no = (Button)findViewById(R.id.btn_no);
String data="";
Intent intent=getIntent();
String jg=intent.getStringExtra("name");
TextView question=(TextView)findViewById(R.id.wenti);
data = jg;
question.setText(data);
}
public void onClick(View view){
switch (view.getId()){
case R.id.btn_yes:
result1();
break;
case R.id.btn_no:
result2();
break;
}
}
private void result2() {
Intent intent = new Intent();
intent.setClass(pingguActivity.this,Tiaozhuan.class);
String btn2=btn_no.getText().toString();
intent.putExtra("result","评估结果:"+btn2);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
private void result1() {
Intent intent = new Intent();
intent.setClass(pingguActivity.this,Tiaozhuan.class);
String btn1=btn_yes.getText().toString();
intent.putExtra("result","评估结果:"+btn1);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
四、String代码
<resources>
<string name="app_name">TiaozhuanApplication</string>
<string name="xm">请输入姓名</string>
<string name="btn_in">进入评估</string>
<string name="btn_out">退出</string>
<string name="jieguo">评估结果</string>
<string name="btn_yes">是</string>
<string name="btn_no">不是</string>
</resources>