android笔记-002启动一个Activity用于输入信息

启动一个Activity用于输入信息

1.在第一个Activity中启动另一个Activity
2.在第二个Activity中输入信息返回给第一个Activity
3.在第二个Activity中进行确定或取消操作需要在第一个Activity中进行判断和提示(取消状态)

android笔记-002启动一个Activity用于输入信息_第1张图片
目录结构

MainActivity布局文件 activity_main.xml





    

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 布局文件




    

    

    

        

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


    MyWish
    写下你的愿望
    提交
    取消
    写下你今天的愿望:
    你取消了写愿望
    你的愿望是:

你可能感兴趣的:(android笔记-002启动一个Activity用于输入信息)