四大组件_Activity_2.通过显示意图启动Activity
程序员文章站
2022-05-09 10:24:43
如果一个Application只有一个Activity的话,Application能创造的价值可就比现在少很多了。我们需要Activity之间能够灵活的跳转,除了跳转,它能够在不同的Activity之间传送数据才算意思。今天我们来写一个活动跳转的demo(账户登录),并侧重了解一下显式意图启动Activity展示结果(不会上传动态图)显式启动Activity当前ActivityIntent intent = new Intent(MainActivity.this,SecondAc...
如果一个Application只有一个Activity的话,Application能创造的价值可就比现在少很多了。我们需要Activity之间能够灵活的跳转,除了跳转,它能够在不同的Activity之间传送数据才算意思。
今天我们来写一个活动跳转的demo(账户登录),并了解一下显式意图启动Activity
-
展示结果(不会上传动态图)
-
显式启动Activity
-
当前Activity
Intent intent = new Intent(MainActivity.this,SecondActivity.class);//参数:当前上下文、要跳转活动的字节码文件
intent.putExtra("account",accountText);//数据传输
intent.putExtra("password",passwordText);
startActivity(intent);//启动Activity
- 跳转Activity方
Intent intent = getIntent();//响应
account = intent.getStringExtra("account");//接收数据
password = intent.getStringExtra("password");
-
来具体看看这个demo
- xml布局文件(activity_main)
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"/>
<EditText
android:id="@+id/account_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"/>
<EditText
android:id="@+id/password_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
<Button
android:id="@+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="立即登录"/>
</LinearLayout>
- activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".SecondActivity">
<TextView
android:id="@+id/account_text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="您的账户是:******"/>
<TextView
android:id="@+id/password_text"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="您的密码是:******"/>
</LinearLayout>
- MainActivity
package com.example.firstactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
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 EditText mAccount;
private EditText mPassword;
private Button mLogin;
private String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();//初始化UI
initListener();//初始化监听
}
private void initListener() {
mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG,"登录被点击");
handleLogin();
}
private void handleLogin() {
String accountText = mAccount.getText().toString().trim();
String passwordText = mPassword.getText().toString().trim();
if (TextUtils.isEmpty(accountText)) {//判断账户是否为空
Toast.makeText(MainActivity.this,"账号为空",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(passwordText)) {//判断密码是否为空
Toast.makeText(MainActivity.this,"密码为空",Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("account",accountText);
intent.putExtra("password",passwordText);
startActivity(intent);
}
});
}
private void initView() {
mAccount = findViewById(R.id.account_edit);
mPassword = findViewById(R.id.password_edit);
mLogin = findViewById(R.id.sign_in_button);
}
}
- SecondActivity
package com.example.firstactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
private TextView mAccount;
private TextView mPassword;
private String account;
private String password;
String TAG = "SecondActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initData();////初始化数据
}
private void initData() {
Intent intent = getIntent();
account = intent.getStringExtra("account");
password = intent.getStringExtra("password");
Log.e(TAG,"account:"+account);
Log.e(TAG,"password:"+password);
initView();//初始化UI
}
private void initView() {
mAccount = findViewById(R.id.account_text);
mPassword = findViewById(R.id.password_text);
mAccount.setText("您的账号是:"+account+"\n");
mPassword.setText("您的密码是:"+password+"\n");
}
}
- 新手提示:不要忘记在AndroidManifest中注册新建的Activity
本文地址:https://blog.csdn.net/weixin_44618862/article/details/107561595
下一篇: 国庆旅游肇庆哪里好玩 肇庆旅游景点推荐