老罗Android视频第一版-------android使用意图传递数据之返回结果

1.在main.xml文件

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/one"
            android:layout_width="90dp"
            android:layout_height="wrap_content" >
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+" >
        </TextView>

        <EditText
            android:id="@+id/two"
            android:layout_width="90dp"
            android:layout_height="wrap_content" >
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="=" >
        </TextView>

        <EditText
            android:id="@+id/res"
            android:layout_width="90dp"
            android:layout_height="wrap_content" >
        </EditText>
    </LinearLayout>

    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="计算结果" >
    </Button>

</LinearLayout>

2.新增一个XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
	<TextView
	    android:id="@+id/msg"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	     ></TextView>
	<EditText 
	    android:id="@+id/three"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    ></EditText>
	</LinearLayout>
	<Button 
	    android:id="@+id/button2"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="返回结果"
	    ></Button>
</LinearLayout>

3.MainActivity

public class MainActivity extends Activity {

	private Button button;
	private final static int REQUESTCODE = 1;
	private EditText one,two,res;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) this.findViewById(R.id.btn);
		one = (EditText) this.findViewById(R.id.one);
		two = (EditText) this.findViewById(R.id.two);
		res = (EditText) this.findViewById(R.id.res);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				int a = Integer.parseInt(one.getText().toString());
				int b = Integer.parseInt(two.getText().toString());
				Intent intent = new Intent(MainActivity.this,OtherActivity.class);
				intent.putExtra("a", a);
				intent.putExtra("b", b);
				startActivityForResult(intent, REQUESTCODE);//表示可以返回的结果
			}
		});
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == 2){
			
			if(requestCode == REQUESTCODE){
				int there = data.getIntExtra("there", 0);
				res.setText(String.valueOf(there));
				
			}
		}
	}
	

}

4.OtherActivity

public class OtherActivity extends Activity {

	private Button button;
	private TextView textView;
	private EditText editText;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_other);
		
		button = (Button) this.findViewById(R.id.button2);
		textView = (TextView) this.findViewById(R.id.msg);
		editText = (EditText) this.findViewById(R.id.three);
		Intent intent = getIntent();
		int a = intent.getIntExtra("a", 0);
		int b = intent.getIntExtra("b", 0);
		textView.setText(a+" + "+b+" = "+" ? ");
		
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				int three = Integer.parseInt(editText.getText().toString());
				intent.putExtra("there", three);
				setResult(2, intent);
				finish();
			}
		});
	}
}


5.清单文件<activity android:name=".OtherActivity"></activity>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值