package com.example.chongzhilianxi; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //点按钮开启第二个窗口 public void click(View v){ Intent intent=new Intent(this,SecondActiviy.class); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { TextView tv1=(TextView)findViewById(R.id.tv1); String z=intent.getStringExtra("您充值了"); String s=intent.getStringExtra("充值失败"); if(requestCode==1) { Switch (the resultCode) { Case 2 : tv1.setText ( "you are the top" + Z); // Toast.makeText (the this, intent.getStringExtra (Z), 0) the .Show (); BREAK ; Case . 3 : tv1.setText ( "recharge failure" ); // Toast.makeText (the this, intent.getStringExtra (S), 0) the .Show (); // Toast.makeText (the this, "recharge failure", 0) .show ( ); BREAK ; } } } }
package com.example.chongzhilianxi; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.Toast; public class SecondActiviy extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); } Public void the Click (View V) { the EditText X = (the EditText) the findViewById (R.id.et1); String the txt1 = x.getText () toString ();. Switch (v.getId ()) { // If the point the buttons are Button2 Case R.id.button2: the Intent the Intent = new new the Intent (); intent.putExtra ( "your top-up" , txt1); setResult ( 2, the Intent); // result code is 2, the result of what the code mark It is which button to press to go back to the Finish (); // close the current window BREAK ; Case R.id.button3: Intent = new new the Intent (); intent.putExtra ( "recharge failure", 0 ); setResult ( . 3, Intent); // result code. 3 finish (); // after finish click on the current window immediately closed, the back window method onActivityResult BREAK ; } } }
<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=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et2" android:layout_centerHorizontal="true" android:layout_marginTop="122dp" android:text="充值" android:onClick="click" /> <EditText android:id="@+id/et2" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="55dp" android:background="#ffffff" android:ems="10" android:paddingLeft="20dp" android:hint="手机号码:" > <requestFocus /> </EditText> <TextView android:id="@+id/tv1" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignLeft="@+id/et2" android:layout_alignRight="@+id/et2" android:layout_below="@+id/button1" android:layout_marginLeft="33dp" android:layout_marginTop="19dp" android:gravity="center" android:textSize="20sp" android:textStyle="italic" /> </RelativeLayout>
<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=".SecondActiviy" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="66dp" android:layout_toLeftOf="@+id/button3" android:onClick="click" android:text="充值" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_alignParentRight="true" android:layout_marginRight="92dp" android:onClick="click" android:text="取消充值" /> <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="110dp" android:background="#ffffff" android:ems="10" android:hint="充值金额:" android:paddingLeft="20dp" > <requestFocus /> </EditText> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button2" android:layout_alignParentTop="true" android:layout_marginTop="61dp" android:gravity="center"= "You To: XXXXX recharge" Android: text android:textSize="20sp" android:textStyle="italic" /> </RelativeLayout>