Android开发之跳转界面
程序开发
2023-09-17 08:01:47
1、通过显示意图跳转界面
第一个Activity(通过Intent携带数据跳转到另一个Activity)
package com.example.activitypractice;import androidx.appcompat.app.AppCompatActivity;import android.content.Context;
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 Button mLogin;private EditText mAccount;private EditText mPassword;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();mLogin.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.d(TAG,"responded to login click");handlerListener();}});}/*** 相应监听事件*/private void handlerListener() {/*** 获取EditText中的值,并将其通过intent传递给SecondActivity,从而实现通信。* 显式意图跳转*/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_LONG).show();return;}Intent intent = new Intent();intent.setClass(this,SecondActivity.class);intent.putExtra("account",account);intent.putExtra("password",password);startActivity(intent);}/***初始化组件*/private void initView() {mAccount = findViewById(R.id.main_account);mPassword = findViewById(R.id.main_password);mLogin = findViewById(R.id.main_login);}
}
第二个Activity(格式请自行通过layout布局)
package com.example.activitypractice;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;/*** 从MainActivity 通过显式意图跳转到SecondActivity*/
public class SecondActivity extends AppCompatActivity {private TextView mAccount;private TextView mPassword;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);init();Intent intent = this.getIntent();if (mAccount != null ){mAccount.setText(intent.getStringExtra("account"));}if (mPassword !=null ){mPassword.setText(intent.getStringExtra("password"));}}private void init() {mAccount = findViewById(R.id.second_account);mPassword = findViewById(R.id.second_password);}
}
结果如下:
2、通过隐式意图跳转界面
第一个Activity
public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";private Button mLogin;private EditText mAccount;private EditText mPassword;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();mLogin.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.d(TAG,"responded to login click");handlerListener();}});}/*** 相应监听事件*/private void handlerListener() {/*** 通过隐式意图跳转到ThirdActivity*/Intent intent = new Intent();intent.setAction("LoginInfo");startActivity(intent);}private void initView() {mAccount = findViewById(R.id.main_account);mPassword = findViewById(R.id.main_password);mLogin = findViewById(R.id.main_login);}
}
第二个Activity
public class ThirdActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_third);}
}
需要为第三个Activity在AndroidManifest.xml中添加过滤(ThirdActivity中的Intent-fliter)
3、Intent和Intent过滤器
Intent是一个消息传递对象,主要的基本用例包括三类。
Intent类型
仅为学习过程中的记录,方便自己以后查阅,也希望能帮助到每一位查阅者。其它Intent详情均可参考Android Developer中的详细介绍。
参考文献地址:https://developer.android.google.cn/guide/components/intents-filters?hl=zh-cn
标签:
上一篇:
vue中是如何监听数组变化(vue图解03)
下一篇:
相关文章
-
无相关信息