跳轉(zhuǎn)頁(yè)面的方式
- Intent i=new Intent(MainActivity.this,SecondActivity.class);
MainActivity.this.startActivity(i); - startActivityForResult(i,1);
案例
布局設(shè)置一個(gè)按鈕奠支,店家跳轉(zhuǎn)頁(yè)面 同時(shí)傳遞數(shù)據(jù) 在第二個(gè)頁(yè)面中也設(shè)置按鈕,點(diǎn)擊將新的值傳遞返回第一頁(yè)面
- 頁(yè)面一
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myapplication.UI.MainActivity"
android:weightSum="1">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffdd02"
android:text="請(qǐng)點(diǎn)擊"/>
</LinearLayout>
- 頁(yè)面二
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="啟動(dòng)二個(gè)Activity" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="按鈕返回?cái)?shù)據(jù)"/>
</LinearLayout>
java代碼功能的實(shí)現(xiàn)
- 頁(yè)面一的功能實(shí)現(xiàn)操作迫筑,點(diǎn)擊按鈕跳轉(zhuǎn)到下一頁(yè)
有倆種方式- 1.startActivity
Button btn1=(Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,SecondActivity.class);
MainActivity.this.startActivity(i);
}
});
- 2.第二種方式 startActivity 傳遞值并且返回值
- 頁(yè)面一java的功能實(shí)現(xiàn)
textView=(TextView) findViewById(R.id.textView);
Log.i(Tag,"創(chuàng)建Activity");
Button btn1=(Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(i,1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == 2){
textView.setText(data.getStringExtra("i"));
}
}
- 頁(yè)面二java的功能實(shí)現(xiàn)
點(diǎn)擊按鈕傳遞數(shù)據(jù)的例子
Button button=(Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent();
i.putExtra("i",content);
setResult(2,i);
finish();
}
});