第七个作业 Activity之间的数据回传

 

 

 

 

 

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click(View v) {
        EditText et1=(EditText)findViewById(R.id.eT1);
        String S= et1.getText().toString();
        Intent intent=new Intent();
        intent.setClass(this,SecondActivity.class);
        intent.putExtra("et1",S);
        startActivityForResult(intent,1);
    }
    
    protected void onActivityResult(int requestCode,int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1&&resultCode==2) {
            TextView tv1=(TextView)findViewById(R.id.tV2);
            String Data1=data.getStringExtra("tV2");
            tv1.setText(Data1);
                
            String Data2=data.getStringExtra("tV3");
            TextView tv2=(TextView)findViewById(R.id.tV3);
            tv2.setText(Data2);
        }
    }

    
}
<TextView
        android:id="@+id/tV1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="手机号码:" />

    <EditText
        android:id="@+id/eT1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tV1"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/tV1"
        android:background="#ffffff"
        android:ems="10" />

    <TextView
        android:id="@+id/tV2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tV1"
        android:layout_alignRight="@+id/cz"
        android:layout_below="@+id/cz"
        android:layout_marginTop="62dp" />

    <Button
        android:id="@+id/cz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/eT1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="122dp"
        android:onClick="click"
        android:text="充值" />

    <TextView
        android:id="@+id/tV3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tV2"
        android:layout_alignRight="@+id/tV2"
        android:layout_below="@+id/tV2"
        android:layout_marginTop="54dp" />
public class SecondActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_main);
        Intent intent=getIntent();
        TextView tv1=(TextView)findViewById(R.id.textView1);
        tv1.setText(intent.getStringExtra("et1")+"的号码充值");
    }
    
    public void CZ(View v) {
            EditText et1=(EditText)findViewById(R.id.editText1);
            String s2=et1.getText().toString();
            Intent intent =new Intent(this,MainActivity.class);
            intent.putExtra("tV2","成功充值"+s2+"块钱");
            setResult(2,intent);
            finish();
            
    }
    public void QX(View v) {
        Intent intent =new Intent(this,MainActivity.class);
        intent.putExtra("tV3","充值失败");
        setResult(2,intent);
        finish();
        
}
    
}
<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignRight="@+id/editText1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView3"
        android:layout_marginLeft="26dp"
        android:layout_marginTop="36dp"
        android:text="充值金额:" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="200dp"
        android:layout_height="20dp"
        android:layout_alignTop="@+id/textView2"
        android:layout_toRightOf="@+id/textView2"
        android:background="#ffffff"
        android:ems="10" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/editText1"
        android:layout_marginBottom="155dp" 
        android:text="取消充值"
        android:onClick="QX"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="154dp" 
        android:text="充值"
        android:onClick="CZ"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:text="你要为:" 
        /

 

posted @ 2019-10-23 17:43  阿点  阅读(346)  评论(0编辑  收藏  举报