《你的第一行Android代码》菜鸡的自学日记-第四天

一.活动的跳转:Intent

1.显式Intent

右键点击com.example.activitytest,创建一个新的Empty Activity(new/Activity/Empty Activity)。

在Second_layout.xml里面修改布局为LinearLayout,添加button控件,命名为Button 2,并修改部分代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2"
        />

</LinearLayout>

回到FirstActivity.java,修改点击事件,使用Intent构造函数中的Intent(Context packageContext,Class<?>cls),所使用的构造函数需要传入两个参数:

  • 第一个参数Context提供一个启动活动的上下文
  • 第二个参数Class用于指定想要启动的目标活动
button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FirstActivity.this,SecondAcitivity.class);
                startActivity(intent);
            }
        });

现在就能实现由FirstActivity到SecondActivity页面的跳转。

显式Intent意图明确指定了要激活的组件是哪个组件,一般是在应用程序内部使用。效率高,耦合度高。

2.隐式Intent

1 )了解隐式Intent
在AndroidManifest.xml中SecondActivity的< intent-filter >标签内添加代码(修改注册信息):

<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
  • < action >标签指明当前活动可响应的action(活动)
  • < category >标签则用来更加精确指明可响应的Intent中可能带有的category(类别)
  • 只有< action >和< category >同时匹配才能响应

修改FirstActivity.java中点击事件的代码:

button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.activitytest.ACTION_START");
                startActivity(intent);
            }
        });

#每个Intent只能指定一个action,但是可以指定多个category
#每新增加一个category,都需要在< intent-filter >标签内添加相应的声明

2 )隐式Intent的更多用法

隐式Intent使得多个应用程序之间的功能共享成为可能(比如转发功能的实现)

使用Intent打开网页。修改FirstActivity中点击事件的代码:

button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://cn.bing.com"));
                startActivity(intent);
            }
        });

此处指定了Intent的action是Intent.ACTION_VIEW(一个Android内置常量),通过Uri.parse()方法将网址解析成Uri对象,然后调用Intent的setData()方法这个Uri对象传出去(打开网页)

#< data >标签用于更加精确的指定当前活动能够响应什么类型的数据

属性作用
android:scheme用于指定数据的协议部分,如http://cn.bing.com中的http部分。
android:host用于指定数据的主机名部分,如http://cn.bing.com中的www.baidu.com部分。
android:port用于指定数据的端口部分,一般紧随在主机名之后,如http://www.rowyer:8080/mypath中的8080部分。
android:path用于指定主机名和端口之后的部分,如一段网址中跟在域名之后的内容,如http://www.rowyer:8080/mypath中的/mypath部分。
android:mimeType用于指定可以处理的数据类型,允许使用通配符的方式进行指定。

在点击BUTTON 1之后会直接跳转到必应搜索页面。

#直观感受action+category+data的三重指定

新建ThirdActivity,修改third_layout.xml中的代码为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 3"
        />
    
</LinearLayout>

在AndroidManifest.xml中修改ThirdActivity的注册信息:

<activity android:name=".ThirdActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="http"/>
            </intent-filter>
        </activity>

在ThirdActivity的< intent-filter >中的配置:

  • 活动能响应的action:intent.action.VIEW
  • 活动的category指定默认的category值
  • < data >标签指定数据的协议必须的http协议

这样ThirdActivity就能像浏览器一样响应一个打开网页的Intent,如图:
手机运行效果
点击浏览器打开必应搜索页面,点击ActivityTest打开ThirdActivity。

Intent还能在程序中调用系统拨号界面,地理位置显示等等,只是指定Intent的action不同,data指定的协议不同。

隐式Intent的效率较低,耦合度低

小结:显式Intent和隐式Intent实现活动跳转。显式Intent直观明了,直接指向跳转的界面;隐式Intent可以通过多重指定来给出符合要求的多个界面以供选择,还可以启动其他程序的活动

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的登录界面代码示例: 1. 首先,在布局文件中创建登录界面的UI元素。可以使用XML编写布局文件,例如: ```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"> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" /> <Button android:id="@+id/login_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Log In" /> </LinearLayout> ``` 2. 然后,在Java类中编写代码。在这个示例中,我们将在点击登录按钮时检查输入的用户名和密码是否正确,并使用Toast显示消息: ```java public class LoginActivity extends AppCompatActivity { private EditText mUsernameEditText; private EditText mPasswordEditText; private Button mLoginButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mUsernameEditText = findViewById(R.id.username); mPasswordEditText = findViewById(R.id.password); mLoginButton = findViewById(R.id.login_button); mLoginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = mUsernameEditText.getText().toString(); String password = mPasswordEditText.getText().toString(); if (username.equals("admin") && password.equals("password")) { Toast.makeText(LoginActivity.this, "Login successful", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(LoginActivity.this, "Invalid username or password", Toast.LENGTH_SHORT).show(); } } }); } } ``` 这是一个非常简单的示例,您可以根据您的需要进行扩展和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值