android intent消息传递出现问题求指导

MainActivity.java
package com.example.haha;

import android.R.id;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
  private Button button;
  private EditText editText1;
  private EditText editText2;
  private EditText editText3;
  private EditText editText4;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(id.button1);
    editText1 = (EditText) findViewById(R.id.editText1);
    editText2 = (EditText) findViewById(R.id.editText2);
    editText3 = (EditText) findViewById(R.id.editText3);
    editText4 = (EditText) findViewById(R.id.editText4);
  }

  @SuppressLint("ShowToast")
  public void onclickonce(View view) {
    // TODO Auto-generated method stub
    Intent intent = new Intent();

    String name = editText1.getText().toString();
    String phone = editText2.getText().toString();
    String addr = editText3.getText().toString();
    String email = editText4.getText().toString();

    intent.setClass(MainActivity.this, secondActivity.class);
    Bundle bundle = new Bundle();

    bundle.putString("name", name);

    bundle.putString("phone", phone);
    bundle.putString("addr", addr);
    bundle.putString("email", email);
    intent.putExtras(bundle);
    // System.out.print("phone");
    startActivity(intent);
    finish();



  }



  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
}

secondActivity.java'
package com.example.haha;

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.example.haha.R.id;

public class secondActivity extends Activity {
  private TextView textView1;
  private TextView textView2;
  private TextView textView3;
  private TextView textView4;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    textView1 = (TextView) findViewById(id.textView1);
    textView2 = (TextView) findViewById(id.textView2);
    textView3 = (TextView) findViewById(id.textView3);
    textView4 = (TextView) findViewById(id.textView4);
    setContentView(R.layout.second);


    /*
     * textView1.setText("name" + bundle.getString("name")); textView2.setText("name");
     * textView3.setText("name"); textView4.setText("name");
     */



  }

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
      case 1:
        switch (resultCode) {
          case 2:

            Intent intent = this.getIntent();

            Bundle bundle = intent.getExtras();

            String name = bundle.getString("name");
            String phone = bundle.getString("phone");
            String addr = bundle.getString("addr");
            String email = bundle.getString("email");
            textView1.setText("" + name);
            textView2.setText(""+phone);
            textView3.setText(""+addr);
            textView4.setText(""+email);
            break;

          default:
            break;
        }
        break;

      default:
        break;
    }

  }

  public boolean onkeydown(int keycode, KeyEvent event) {
    if (keycode == KeyEvent.KEYCODE_BACK) {
      Intent intent = new Intent();
      intent.setClass(secondActivity.this, MainActivity.class);
      startActivity(intent);
    }
    return super.onKeyDown(keycode, event);
  }

  public void onClick_ShowMyActivity(View view) {
    Intent intent = new Intent(this, MainActivity.class);
    startActivityForResult(intent, 2);
  }

}


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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="com.example.haha.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView1"
        android:layout_marginLeft="21dp"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/editText1"
        android:text="电话" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText1"
        android:layout_alignTop="@+id/textView2"
        android:ems="10"
        android:inputType="phone" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="37dp"
        android:text="返回" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="18dp"
        android:text="住址"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="26dp"
        android:text="e-mail" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/editText1"
        android:ems="10"
        android:inputType="textPostalAddress" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText4"
        android:layout_marginTop="62dp"
        android:text="保存" 
        android:onClick="onclickonce"/>

    <EditText
        android:id="@+id/editText4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText3"
        android:layout_alignTop="@+id/textView4"
        android:ems="10"
        android:inputType="textEmailAddress" />

</RelativeLayout>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值