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