启动一个Activity用于输入信息
1.在第一个Activity中启动另一个Activity
2.在第二个Activity中输入信息返回给第一个Activity
3.在第二个Activity中进行确定或取消操作需要在第一个Activity中进行判断和提示(取消状态)
MainActivity布局文件 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.sky.mywish.MainActivity">
<Button
android:id="@+id/btnWriteWish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnWriteWish" />
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
MainActivity java 代码
package com.example.sky.mywish;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView tvResult;
private Button btnWriteWish;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = findViewById(R.id.tvResult);
btnWriteWish = findViewById(R.id.btnWriteWish);
btnWriteWish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,AnotherActivity.class);
startActivityForResult(i,2);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode){
case 1:
tvResult.setText("你的愿望是:" + data.getStringExtra("data"));
break;
case 0:
Toast.makeText(MainActivity.this,R.string.cancel,Toast.LENGTH_SHORT).show();
break;
}
}
}
AnotherActivity 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.sky.mywish.AnotherActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tvWriteWish"
android:textSize="20dp"/>
<EditText
android:id="@+id/etWriteWish"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnSubmit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/btnSubmit" />
<Button
android:id="@+id/btnCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/btnCancel" />
</LinearLayout>
</LinearLayout>
AnotherActivity java代码
package com.example.sky.mywish;
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;
public class AnotherActivity extends AppCompatActivity {
private Button btnSubmit,btnCancel;
private EditText etWriteWish;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
btnSubmit = findViewById(R.id.btnSubmit);
btnCancel = findViewById(R.id.btnCancel);
etWriteWish = findViewById(R.id.etWriteWish);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.putExtra("data",etWriteWish.getText().toString());
setResult(1,i);
finish();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(0,getIntent());
finish();
}
});
}
}
string.xml
<resources>
<string name="app_name">MyWish</string>
<string name="btnWriteWish">写下你的愿望</string>
<string name="btnSubmit">提交</string>
<string name="btnCancel">取消</string>
<string name="tvWriteWish">写下你今天的愿望:</string>
<string name="cancel">你取消了写愿望</string>
<string name="wish">你的愿望是:</string>
</resources>