一、通过显式意图来实现Activity间的跳转
显式意图是指在创建Intent对象时就指定接受者组件
/**
* 下面是通过显式意图进行跳转,即明确写出要跳转到SecondActivity.class组件中去
*/
Intent intent =new Intent(this,SecondActivity.class);
intent.putExtra("account",account);
intent.putExtra("password",password);
startActivity(intent);
注意:创建Activity时要在manifests里进行静态注册,示例如下:
<activity android:name=".SecondActivity"> </activity>
之后再要跳转到的界面接受Intent传递的内容
//通过getIntent获取MainActivity传来的intent
Intent intent = getIntent();
String account = intent.getStringExtra("account");
String password = intent.getStringExtra("password");
点击登录按钮
二、通过隐式意图来实现Activity间的跳转
隐式意图就是通过intent过滤器来进行匹配跳转
/**
* 下面是通隐式意图进行跳转,要在manifests里添加意图过滤
*/
Intent intent = new Intent();
intent.setAction("com.example.activitydemo.LoginInfo");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra("account",account);
intent.putExtra("password",password);
startActivity(intent);
进行注册的同时添加intent过滤
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.activitydemo.LoginInfo"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
点击登录
三、原码
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitydemo"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.activitydemo.LoginInfo"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application> </manifest>
MainActivity.java
package com.example.activitydemo; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity";
private EditText mAccount;
private EditText mPassword;
private Button mLogin; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView();
initListener();
} private void initListener() {
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//登录按钮被点击了
Log.d(TAG,"Login Click。。。");
handlerLogin();
}
});
} private void handlerLogin() {
//.trim()用于去空格
String account = mAccount.getText().toString().trim();
if (TextUtils.isEmpty(account)) {
Toast.makeText(this,"输入的账号为空",Toast.LENGTH_SHORT).show();
return;
} String password = mPassword.getText().toString().trim();
if (TextUtils.isEmpty(password)) {
Toast.makeText(this,"输入的密码为空",Toast.LENGTH_SHORT).show();;
}
//先要创建一个意图对象,然后通过StartActivity()来实现跳转
/**
* 下面是通过显式意图进行跳转,即明确写出要跳转到SecondActivity.class组件中去
*/
// Intent intent =new Intent(this,SecondActivity.class);
// intent.putExtra("account",account);
// intent.putExtra("password",password);
// startActivity(intent); /**
* 下面是通隐式意图进行跳转,要在manifests里添加意图过滤
*/
Intent intent = new Intent();
intent.setAction("com.example.activitydemo.LoginInfo");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra("account",account);
intent.putExtra("password",password);
startActivity(intent);
} private void initView() {
mAccount = (EditText) this.findViewById(R.id.account);
mPassword = (EditText) this.findViewById(R.id.password);
mLogin = (Button) this.findViewById(R.id.login);
}
}
activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <TextView
android:layout_width="wrap_content"
android:text="登录"
android:textSize="30sp"
android:layout_gravity="center"
android:layout_height="wrap_content"> </TextView> <TextView
android:layout_width="wrap_content"
android:text="账号:"
android:textSize="25sp"
android:layout_height="wrap_content"> </TextView> <EditText
android:id="@+id/account"
android:layout_width="match_parent"
android:layout_height="wrap_content"> </EditText> <TextView
android:layout_width="wrap_content"
android:text="密码:"
android:textSize="25sp"
android:layout_height="wrap_content"> </TextView>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:inputType="textPassword"
android:layout_height="wrap_content"> </EditText>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:text="登录"
android:layout_height="wrap_content"> </Button>
</LinearLayout>
SecondActivity.java
package com.example.activitydemo; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; import androidx.annotation.Nullable; public class SecondActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); TextView info =(TextView) this.findViewById(R.id.info);
//通过getIntent获取MainActivity传来的intent
Intent intent = getIntent();
String account = intent.getStringExtra("account");
String password = intent.getStringExtra("password"); info.setText("您的账号为:"+account+"您的密码为:"+password);
}
}
activity_second_.xml
<?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:layout_width="match_parent"
android:text="登录信息如下:"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:layout_height="wrap_content"> </TextView>
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:textSize="25sp"
android:text=""
android:layout_height="wrap_content"> </TextView>
</LinearLayout>