• Android intent的一些小使用


    一些基本的问题就不进行说明了,直接上代码!!!

    // 最后的隐形intent和带返回值没有解决!!!

    1. Test5.java

    package com.example.myapplication;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.ComponentName;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class Test5 extends AppCompatActivity {
    
        Button Call, Browser, Note, Set, Photo, JumpDesket, XianNo, GetReturn, GetNumber, Yin;//设置一些按钮名称
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test5);
            // 对按钮进行赋予功能
            Call = findViewById(R.id.buttonCall);
            Note = findViewById(R.id.buttonNote);
            Browser = findViewById(R.id.buttonBrowser);
            Set = findViewById(R.id.buttonSet);
            Photo = findViewById(R.id.buttonPhotograph);
            JumpDesket = findViewById(R.id.buttonJumpDesk);
            XianNo = findViewById(R.id.buttonXianNo);
            GetReturn = findViewById(R.id.buttonGetReturn);
            GetNumber = findViewById(R.id.buttonGet);
            Yin = findViewById(R.id.buttonYin);
            // 赋予点击能力
            Call.setOnClickListener(onClickListener);
            Note.setOnClickListener(onClickListener);
            Browser.setOnClickListener(onClickListener);
            Set.setOnClickListener(onClickListener);
            Photo.setOnClickListener(onClickListener);
            JumpDesket.setOnClickListener(onClickListener);
            XianNo.setOnClickListener(onClickListener);
            GetReturn.setOnClickListener(onClickListener);
            GetNumber.setOnClickListener(onClickListener);
            Yin.setOnClickListener(onClickListener);
        }
    
        // 对按钮进行写方法
        private final View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {//点击事件
                Button button = (Button) view;   //把点击获得的id信息传递给button
                try {
                    if (button.getId() == R.id.buttonCall) { // 打电话
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_DIAL); //调用拨号面板
                        intent.setData(Uri.parse("tel:110")); //设置要拨打的号码
                        startActivity(intent);
                    } else if (button.getId() == R.id.buttonNote) {  // 发短信
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_SENDTO); //调用发送短信息
                        intent.setData(Uri.parse("smsto:110XXXXXX")); //设置要发送的号码,自己设计就好
                        intent.putExtra("sms_body", "Welcome to Android!"); //设置要发送的信息内容
                        startActivity(intent);
                    } else if (button.getId() == R.id.buttonBrowser) { // 浏览器
                        Intent intent = new Intent();
                        // 设置浏览器地址
                        intent.setData(Uri.parse("https://blog.csdn.net/weixin_51395608?type=blog"));
                        // 设置动作
                        intent.setAction(Intent.ACTION_VIEW);
                        startActivity(intent);
                    } else if (button.getId() == R.id.buttonSet) { // 设置
                        Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
                        startActivity(intent);
                    } else if (button.getId() == R.id.buttonPhotograph) {  // 拍照
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                        startActivity(intent);
                    } else if (button.getId() == R.id.buttonJumpDesk) {    // 跳转到桌面
                        Intent intent = new Intent();
                        // 设置action动作属性
                        intent.setAction(Intent.ACTION_MAIN);
                        // 设置categoty种类显示主屏幕
                        intent.addCategory(Intent.CATEGORY_HOME);
                        startActivity(intent);
                    } else if (button.getId() == R.id.buttonXianNo) {  // 显性不带参数
                        Intent intent = new Intent(Test5.this, MainActivity.class);
                        startActivity(intent);
                    } else if (button.getId() == R.id.buttonYin) { // 隐形  待完成
                        Intent intent = new Intent();
                        // 未完成
                        startActivity(intent);
                    } else if (button.getId() == R.id.buttonGet) { // 带参数
                        Intent intent = new Intent(Test5.this, Empty.class);
                        intent.putExtra("data", "我带着一些数据来了!!!");
                        startActivity(intent);
                    } else if (button.getId() == R.id.buttonGetReturn) {   // 带返回值 
                    // 未完成!!!!
                        Intent intent = new Intent(Test5.this, Empty.class);    //跳转设置
                        Bundle extras = new Bundle();
                        extras.putString("key", "value");
    //                    startActivityForResult(intent, 1, extras);          //带返回的跳转  requestCode = 1
                    } else {
                        Log.e("orror", "未知按钮组件调用");
                    }
                } catch (Exception e) {
                    Log.e("No", "main,error");
                }
            }
        };
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    2. activity_main5.xml

    
    <androidx.constraintlayout.widget.ConstraintLayout 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"
        tools:context=".Test5">
    
    
        <TextView
            android:id="@+id/textView4"
            android:layout_width="276dp"
            android:layout_height="71dp"
            android:text="Intent的使用"
            android:textSize="48sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="17dp" />
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="282dp"
            android:layout_height="495dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.496"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.444">
    
            <Button
                android:id="@+id/buttonCall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="打电话"
                tools:layout_editor_absoluteX="24dp"
                tools:layout_editor_absoluteY="104dp" />
    
            <Button
                android:id="@+id/buttonNote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="发短信"
                tools:layout_editor_absoluteX="24dp"
                tools:layout_editor_absoluteY="152dp" />
    
            <Button
                android:id="@+id/buttonBrowser"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="打开浏览器"
                tools:layout_editor_absoluteX="25dp"
                tools:layout_editor_absoluteY="200dp" />
    
            <Button
                android:id="@+id/buttonSet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="设置"
                tools:layout_editor_absoluteX="24dp"
                tools:layout_editor_absoluteY="248dp" />
    
            <Button
                android:id="@+id/buttonPhotograph"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拍照"
                tools:layout_editor_absoluteX="24dp"
                tools:layout_editor_absoluteY="296dp" />
    
            <Button
                android:id="@+id/buttonJumpDesk"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="转到桌面"
                tools:layout_editor_absoluteX="24dp"
                tools:layout_editor_absoluteY="344dp" />
    
            <Button
                android:id="@+id/buttonXianNo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="打开另一个Activity(显性,不带参数)"
                tools:layout_editor_absoluteX="24dp"
                tools:layout_editor_absoluteY="394dp" />
    
            <Button
                android:id="@+id/buttonYin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="打开另一个Activity(隐性)"
                tools:layout_editor_absoluteX="24dp"
                tools:layout_editor_absoluteY="442dp" />
    
            <Button
                android:id="@+id/buttonGet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="带参数打开另一个Activity"
                tools:layout_editor_absoluteX="24dp"
                tools:layout_editor_absoluteY="490dp" />
    
            <Button
                android:id="@+id/buttonGetReturn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="带返回值打开另一个Activity"
                tools:layout_editor_absoluteX="25dp"
                tools:layout_editor_absoluteY="540dp" />
        LinearLayout>
    
    androidx.constraintlayout.widget.ConstraintLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113

    效果(没有专门去设计):
    效果

    3. Empty.java (这个是用来带参数打开Activity按钮用的)

    package com.example.myapplication;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.annotation.SuppressLint;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;
    
    public class Empty extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_empty);
            String currentType = getIntent().getStringExtra("data");
            TextView t = findViewById(R.id.textVE);
            t.setText(currentType);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    4. activity_empty.xml

    
    <androidx.constraintlayout.widget.ConstraintLayout 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"
        tools:context=".Empty">
    
        <TextView
            android:id="@+id/textVE"
            android:layout_width="243dp"
            android:layout_height="361dp"
            android:background="#FFFFFF"
            android:text="TextView"
            android:textColor="#4A4A4A"
            android:textSize="48sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.283" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回值"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textVE"
            app:layout_constraintVertical_bias="0.419" />
    androidx.constraintlayout.widget.ConstraintLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    效果:
    效果

    5. 总结

    不是很难!!!

  • 相关阅读:
    23种设计模式
    Python Record
    用队列实现栈-力扣
    电脑如何去掉u盘写保护的状态
    keepalived原理以及lvs、nginx跟keeplived的运用
    用VS Code搞Qt6:编译源代码与基本配置
    gin框架中使用websocket发送消息及群聊
    神经元结构图简笔画,神经组织图片简笔画
    程序员必看内容连续集之 SpringBoot03 SSM整合SpringBoot
    CT0515是一款完善的单节锂电池恒流/恒压线性充电管理芯片
  • 原文地址:https://blog.csdn.net/weixin_51395608/article/details/134078991