package com.example.qerw; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private Button button; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view) { button = (Button) findViewById(R.id.button3); editText = (EditText) findViewById(R.id.edittext); String edittext = editText.getText().toString(); Intent intent = new Intent(MainActivity.this, secondActivity.class); intent.putExtra("sjhm", edittext); startActivityForResult(intent, 1); } protected void onActivityResult(int requestCode, int resultCode, Intent czje ) { super.onActivityResult(requestCode, resultCode, czje); if (requestCode == 1 && resultCode == 2) { String acdate = czje.getStringExtra("czje"); String cz = "您充值了" + acdate + "元"; Toast.makeText(MainActivity.this, cz, Toast.LENGTH_LONG).show();; } else if (requestCode == 1 && resultCode == 4) { String accdate = "充值失败"; Toast.makeText(MainActivity.this, accdate, Toast.LENGTH_LONG).show(); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package com.example.qerw; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class secondActivity extends ActionBarActivity { private TextView textView; private EditText editText; private Button button1,button2; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondactivity); textView=(TextView)findViewById(R.id.textview2); Intent intent=getIntent(); String SJHM=intent.getStringExtra("sjhm"); textView.setText(SJHM); } public void click(View view){ button1=(Button)findViewById(R.id.button1); editText=(EditText)findViewById(R.id.edittext1); String CZJE=editText.getText().toString(); Intent intent=new Intent(); intent.putExtra("czje",CZJE); setResult(2,intent); finish(); } public void onclick(View view){ button2=(Button)findViewById(R.id.button2); Intent intent=new Intent(); setResult(4,intent); finish(); } }
<?xml version="1.0" encoding="utf-8"?> <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.qerw.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="手机号码" android:layout_weight="1" /> <EditText android:id="@+id/edittext" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" /> </LinearLayout> <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="充值" android:layout_centerInParent="true" android:onClick="click" /> </RelativeLayout> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 <?xml version="1.0" encoding="utf-8"?> <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.qerw.MainActivity" > <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="您要为:" /> <TextView android:layout_centerInParent="true" android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/textview1" /> <TextView android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/textview2" android:text="充值" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/textview1" android:id="@+id/line" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="充值金额" android:layout_weight="1" /> <EditText android:id="@+id/edittext1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" /> </LinearLayout> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/line" android:text="充值" android:onClick="click" /> <TextView android:layout_toRightOf="@id/button1" android:id="@+id/textview4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/line" android:layout_toRightOf="@id/textview4" android:text="取消充值" android:onClick="onclick" /> </RelativeLayout>