Android:week 10总结 显隐式启动、拨号显示、数据相关操作

本文详细介绍Android应用中不同活动的启动方式,包括隐式和显示启动,以及如何使用拨号功能。此外,还深入探讨了如何在应用内部保存、读取和删除数据,包括通过SharedPreferences进行数据存储的方法。
摘要由CSDN通过智能技术生成

目录

 

Monday

1.隐式启动、显示启动、拨号显示

 2.获取输入数据

Tuesday

1.保存、读取、删除数据


Monday

1.隐式启动、显示启动、拨号显示

mainActivity:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //显式启动
    /* 步骤:
        1.创建第二个Activity和layout 并进行绑定
        2.在AndroidMainfest.xml中加入<Activity>
        3,Intent()--> setClass(this,XXXActivity.class) --> startActivity(intent)

    public void click(View view){
        Intent intent = new Intent(this,SecondActivity.class);
        startActivity(intent);
    }*/

    //隐式启动
    /* 步骤:
            1.AndroidMainfest.xml中的<Activity>加入<category>和<action>
            2.Intent语句内容

    public void click(View view){
        Intent intent = new Intent(Intent.ACTION_SENDTO);//通用的 或者"android.intent.action.SENDTO"
        startActivity(intent);
    }*/
    //拨号键
    public void click(View view){
        Intent intent = new Intent(Intent.ACTION_DIAL);//通用的 或者"android.intent.action.SENDTO"
        intent.setData(Uri.parse("tel:123"));
        startActivity(intent);
    }
}

 AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".SecondActivity"
            android:label="22222">

            <intent-filter>
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
        </activity>
        <activity android:name=".ThirdActivity" android:label="33333">>
            <intent-filter>
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
        </activity>
    </application>

</manifest>

 SecondActicity:

package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
    }
}

second_activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SecondActivity"
        android:textSize="15sp">
    </TextView>
</LinearLayout>

 2.获取输入数据

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:id="@+id/filename"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/tv">
    </EditText>
    <EditText
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/filename"
        android:singleLine="true">
    </EditText>
    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/text"
        app:layout_constraintLeft_toLeftOf="@id/text"
        android:text="保存"
        android:textSize="15sp"
        android:onClick="click">
    </Button>
    <Button
        android:id="@+id/read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/save"
        app:layout_constraintLeft_toRightOf="@id/save"
        android:text="读取"
        android:textSize="15sp"
        android:onClick="click">
    </Button>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java: 

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText filename,fileContent;
    private Button save,read;
    @Override
    //设置监听器 实现相关方法
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        filename=findViewById(R.id.filename);
        fileContent=findViewById(R.id.text);
        save=findViewById(R.id.save);
        read=findViewById(R.id.read);
        filename.setOnClickListener(this);
        fileContent.setOnClickListener(this);
    }

    public void onClick(View view){

    }

    public void click(View view){
        switch (view.getId()){
            case R.id.save:
                //文件存在data中
                FileOutputStream fileOutputStream=null;
                try{
                    fileOutputStream = this.openFileOutput(filename.getText().toString(), this.MODE_PRIVATE);
                    fileOutputStream.write(fileContent.getText().toString().getBytes());
                    Toast.makeText(this,"输入数据成功",Toast.LENGTH_SHORT).show();
                }catch (Exception e){
                    e.printStackTrace();
                    Toast.makeText(this,"输入数据失败",Toast.LENGTH_SHORT).show();
                    try {
                        fileOutputStream.close();
                    }catch (Exception ex){
                        ex.printStackTrace();
                    }
                }
                break;
            case R.id.read:
                break;
                case 
        }
    }
}

Tuesday

1.保存、读取、删除数据

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名"
        android:textSize="15sp"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:textSize="15sp"
        app:layout_constraintTop_toBottomOf="@+id/name" />
    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/password"
        app:layout_constraintLeft_toLeftOf="@+id/password"
        android:text="保存"
        android:onClick="click"
        />
    <Button
        android:id="@+id/read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/password"
        app:layout_constraintLeft_toRightOf="@+id/save"
        android:text="读取"
        android:onClick="click"
        />
    <Button
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/password"
        app:layout_constraintLeft_toRightOf="@+id/read"
        android:text="删除"
        android:onClick="click"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity:

package cn.rjxy.sharepreference;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText name, password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = findViewById(R.id.name);
        password = findViewById(R.id.password);
    }
    public void click(View view){
        SharedPreferences sp= null;
        SharedPreferences.Editor editor = null;
        switch (view.getId()){
            case R.id.save:
                sp = getSharedPreferences("data", MODE_PRIVATE);
                editor = sp.edit();
                //键值和名称
                editor.putString("Name", name.getText().toString());
                editor.putString("Password", password.getText().toString());
                //将保存的数据提交
                editor.commit();
                break;
            case R.id.read:
                sp = getSharedPreferences("data", MODE_PRIVATE);
                //获取名称
                String data = sp.getString(name.getText().toString(), "");
                Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                sp = getSharedPreferences("data", MODE_PRIVATE);
                editor = sp.edit();
                //删除信息
                editor.remove(name.getText().toString());
                editor.commit();
                break;
        }
    }
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值