package com.example.chuangkou; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.EditText; 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,MainActivity2.class); EditText et2=(EditText)findViewById(R.id.et2); String s=et2.getText().toString(); intent.putExtra("et1",s ); 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(resultCode){ case 2: tv1.setText("您充值了"+z); //Toast.makeText(this,intent.getStringExtra(z), 0).show(); break; case 3: tv1.setText("充值失败"); //Toast.makeText(this,intent.getStringExtra(s), 0).show(); //Toast.makeText(this,"充值失败", 0).show(); break; } } } }
package com.example.chuangkou; 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.TextView; import android.widget.Toast; public class MainActivity2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); TextView tv=(TextView)findViewById(R.id.tv2); Intent intent =getIntent(); tv.setText(intent.getStringExtra("et1")+"的号码充值"); } public void click(View v) { EditText x=(EditText)findViewById(R.id.et1); String txt1=x.getText().toString(); switch (v.getId()) { //如果点的按钮是button2 case R.id.button2: Intent intent=new Intent(); intent.putExtra("您充值了", txt1); setResult(2, intent);//结果码是2,结果码标记一下是按哪个按钮回去的 finish();//关闭当前窗口 break; case R.id.button3: intent=new Intent(); intent.putExtra("充值失败", 0); setResult(3, intent);//结果码为3 finish();//finish点击后当前窗口立即关闭,回到上个窗口的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_alignBottom="@+id/et1" android:layout_alignLeft="@+id/et1" android:layout_marginBottom="66dp" android:gravity="center" android:text="您要为:" android:textSize="20sp" android:textStyle="italic" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/tv1" android:layout_alignRight="@+id/et1" android:layout_toRightOf="@+id/button2" /> </RelativeLayout>