Activity页面跳转
Activity页面跳转
- 很高兴和大家再次见面,我是lady_zhou,这是我的Blog,欢迎大家一起来学习Activity页面跳转
Activity页面跳转有两种方式:
- 无返回结果的页面跳转
- 有返回结果的页面跳转
无返回结果的页面跳转
主要代码实现:
Intent intent = new Intent(上下文,你将要跳的Activity.class);
startActivity(intent);
大致情况就是:
Intent intent = new Intent(MainActivity.this, NewActivity.class);
startActivity(intent);
这里是从MainActivity调到NewActivity
有返回结果的页面跳转
主要代码实现:
从第一个Activity跳到第二个Activity
在第一个Activity中:
Intent intent = new Intent(上下文,你将要跳的Activity.class);
startActivityForResult(intent, requestCode);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
在第二个Activity中
setResult(resultCode,intent);
这里的参数分别代表:
- requestCode:请求的标志
- resultCode:第二个页面返回的标志
- data:回传的数据
下面举两个例子,让我们再来熟悉一下具体操作
1.这里是在第一个界面中输入两个数相乘,在第二个界面中显示结果
MainActivity:
private Button mButton;
private EditText mEditText1, mEditText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button);
mEditText1 = (EditText) findViewById(R.id.edit1);
mEditText2 = (EditText) findViewById(R.id.edit2);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,NewActivity.class);
Bundle bundle = new Bundle();
bundle.putDouble("key1",Double.valueOf(mEditText1.getText().toString()));
bundle.putDouble("key2",Double.valueOf(mEditText2.getText().toString()));
Log.d("one",mEditText1.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
activity_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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit1"
android:textSize="40dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乘以"
android:textSize="40dp"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit2"
android:textSize="40dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/text1"
android:text="="
android:textSize="40dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/text2"
android:textSize="40dp"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="等于"
android:layout_gravity="center"
android:textSize="40dp"
/>
</LinearLayout>
NewActivity:
public class NewActivity extends Activity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
mTextView = (TextView) findViewById(R.id.text);
Bundle bundle = getIntent().getExtras();
Double mEditText1 = bundle.getDouble("key1");
Double mEditText2 = bundle.getDouble("key2");
Double three = mEditText1 * mEditText2;
mTextView.setText("结果:" + String.valueOf(three));
}
}
activity_new.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:textSize="40dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="返回"
android:layout_gravity="center"
/>
</LinearLayout>
2.这里是在第一个界面中输入两个数相乘,在第二个界面中显示结果,并把结果返回到第一个界面中
NewActivity:
public class NewActivity extends Activity {
private TextView mTextView;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
mTextView = (TextView) findViewById(R.id.text);
mButton = (Button) findViewById(R.id.button);
Bundle bundle = getIntent().getExtras();
Double mEditText1 = bundle.getDouble("key1");
Double mEditText2 = bundle.getDouble("key2");
final Double three = mEditText1 * mEditText2;
mTextView.setText("结果:" + String.valueOf(three));
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("data",Double.valueOf(three));
setResult(2,intent);
finish();
}
});
}
}
MainActivity:
private Button mButton;
private EditText mEditText1, mEditText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button);
mEditText1 = (EditText) findViewById(R.id.edit1);
mEditText2 = (EditText) findViewById(R.id.edit2);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, NewActivity.class);
Bundle bundle = new Bundle();
bundle.putDouble("key1",Double.valueOf(mEditText1.getText().toString()));
bundle.putDouble("key2",Double.valueOf(mEditText2.getText().toString()));
intent.putExtras(bundle);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == 2) {
Double result = data.getDoubleExtra("data",2);
mTextView.setText(String.valueOf(result));
}
}
}
XML文件都是相同的,这样就能够实现两个界面的跳转了。