初学组件一 Activity

本文介绍了Android初学者如何使用Activity创建界面,并通过intent在Activity间传递数据,重点讲解了intent过滤器的作用及配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初学组件一 Activity

1.Activity 作用

​ Activity表示一个界面,一个界面表示一个Activity。

2.Activity的使用

​ 要使用Activity,必须要在清单中配置Activity以及该特有的属性。

​ 清单文件为 AndroidManifest.xml。

​ 一个Activity需要定义一个元素,例如下面代码所示:

<application ...>

​	<activity android:name=".ExampleActivity"/>

</application>

​ 如还需要进一步看它其他属性,那么去官方网站自己look吧。

3.intent过滤器

​ intent是个很叼的容器,使用它可以将数据在各个Activity之间传递,然后这个玩意的使用,需要在清单中的中声明 属性,最基本的元素包括了,,等,

    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

​ 表示main入口,也就是主入口,一打开app就会进入这个Activity,

​ 表示能够接受请求,额,更具体一点的话,还不了解,再看。

​ 这里先稍微详细一点介绍一下intent过滤器,首先发出这两个代码块。

        //跳转到另外一个activity,通过显示意图intent指定的class类。
     Intent intent = new Intent(this,SecondActivity.class);
     intent.putExtra("account", accountText);
     intent.putExtra("password", passwordText);
     startActivity(intent);

​ 以上是通过显示意图来对intent进行操作,在new的时候,需要传入上下文 this,还有指定跳转的Activity的字节码,

Intent intent = new Intent(this,SecondActivity.class),最后启动startActivity(intent)便可开启另外的Activity

    //通过隐式意图来指定class类
    Intent intent = new Intent();
    intent.setAction("android.intent.action.LOGIN");
    intent.addCategory(intent.CATEGORY_DEFAULT);
    intent.putExtra("account",accountText);
    intent.putExtra("password",passwordText);
    startActivity(intent);

 以上是通过隐式意图来对intent进行操作,在new的时候,不需要传入任何的参数,但是需要设置一下清单里边对应的intent-filter,先来看看清单里边的设置。
 <activity android:name=".ThirdActivity">
            <intent-filter>
                <action android:name="android.intent.action.LOGIN"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
 </activity>

所以隐式意图需要通过设置action,通过添加 intent.setAction(“android.intent.action.LOGIN”) 的setAction方法和添加intent.addCategory(intent.CATEGORY_DEFAULT)的addCategory。

ok,差不多先上一个完整的代码先:

MainActivity.java

package com.example.mysecondapp;

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 EditText mAccount;
    private EditText mPassword;
    private Button mLogin;
    private static final String TAG ="MainActivity";
    
    @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");
                handerlogin();
            }
        });
    }
    
    private void handerlogin(){
        //trim()去空格
        String accountText = mAccount.getText().toString().trim();
        if (TextUtils.isEmpty(accountText)) {
            Toast.makeText(this, "您输入的账号为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String passwordText = mPassword.getText().toString().trim();
        if(TextUtils.isEmpty(passwordText)){
            Toast.makeText(this,"您输入的密码为空", Toast.LENGTH_SHORT).show();
            return;
        }

//        //跳转到另外一个activity,通过显示意图intent指定的class类。
//        Intent intent = new Intent(this,SecondActivity.class);
//        intent.putExtra("account", accountText);
//        intent.putExtra("password", passwordText);
//        startActivity(intent);
        

        //通过隐式意图来指定class类
        Intent intent = new Intent();
        intent.setAction("android.intent.action.LOGIN");
        intent.addCategory(intent.CATEGORY_DEFAULT);
        intent.putExtra("account",accountText);
        intent.putExtra("password",passwordText);
        startActivity(intent);
    }
    
    private void initView() {
        mAccount = this.findViewById(R.id.account);
        mPassword = this.findViewById(R.id.password);
        mLogin = this.findViewById(R.id.login);
    }

}

ThirdActivity.java

package com.example.mysecondapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;

public class ThirdActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);

        TextView mAccount = this.findViewById(R.id.loginGet);
        TextView mPassword = this.findViewById(R.id.passwordGet);
    
        Intent intent = getIntent();
    
        String account_get = intent.getStringExtra("account");
        String password_get = intent.getStringExtra("password");
    
        mAccount.setText(account_get);
        mPassword.setText(password_get);
    
    }

}

activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<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:text="账号:"/>
    <EditText
        android:id="@+id/account"
        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"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"/>

</LinearLayout>

activity_third.xml

<?xml version="1.0" encoding="utf-8"?>
<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:text="您的账号是:"/>
    <TextView
        android:id="@+id/loginGet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="xxxx"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="您的密码是"/>
    <TextView
        android:id="@+id/passwordGet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="xxxxx"/>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值