Android:week 10總結 顯隱式啓動、撥號顯示、數據相關操作

目錄

 

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;
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章