數(shù)據(jù)存儲的幾種方式:
布局:
<?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"
android:padding="15dp">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="輸入內(nèi)容"/>
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"
android:layout_marginTop="10dp"
android:textAllCaps="false"/>
<Button
android:id="@+id/btn_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="顯示"
android:layout_marginTop="10dp"
android:textAllCaps="false"/>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
</LinearLayout>
因為sdk版本在23以后需要動態(tài)申請系統(tǒng)讀寫權(quán)限距辆,所以首先在Mainfest配置里加上:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
然后在APP啟動時加上權(quán)限
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
1.SharedPreferences:
public class SharedPreferencesActivity extends AppCompatActivity {
private EditText ev_name;
private Button save,show;
private TextView content;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences);
ev_name = findViewById(R.id.et_name);
save = findViewById(R.id.btn_save);
show = findViewById(R.id.btn_show);
content = findViewById(R.id.tv_show);
sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
editor = sharedPreferences.edit();
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.putString("name",ev_name.getText().toString());
editor.apply();
}
});
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
content.setText(sharedPreferences.getString("name",""));
}
});
}
}
2.File內(nèi)部存儲
public class FileActivity extends AppCompatActivity {
private EditText ev_name;
private Button save,show;
private TextView content;
private final String filename = "test.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
ev_name = findViewById(R.id.et_name);
save = findViewById(R.id.btn_save);
show = findViewById(R.id.btn_show);
content = findViewById(R.id.tv_show);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save(ev_name.getText().toString().trim());
}
});
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
content.setText(read());
}
});
}
//存儲數(shù)據(jù)
private void save(String content){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = openFileOutput(filename,MODE_PRIVATE);
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//讀取數(shù)據(jù)
private String read(){
FileInputStream fileInputStream = null;
try {
fileInputStream = openFileInput(filename);
byte[] buff = new byte[1024];
StringBuffer sb = new StringBuffer(); //可實現(xiàn)字符串拼接
Log.d("path",Environment.getDataDirectory()+"");
int len = 0;
while((len = fileInputStream.read(buff))>0){
sb.append(new String(buff,0,len));
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
{
if(fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return null;
}
}
3.file外部存儲:
public class FileOutActivity extends AppCompatActivity {
private EditText ev_name;
private Button save,show;
private TextView content;
private final String filename = "test.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_out);
ev_name = findViewById(R.id.et_name);
save = findViewById(R.id.btn_save);
show = findViewById(R.id.btn_show);
content = findViewById(R.id.tv_show);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save(ev_name.getText().toString().trim());
}
});
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
content.setText(read());
}
});
}
//存儲數(shù)據(jù)
private void save(String content){
FileOutputStream fileOutputStream = null;
try {
// fileOutputStream = openFileOutput(filename,MODE_PRIVATE);
File dir = new File(Environment.getExternalStorageDirectory(),"peng");
if(!dir.exists()){
dir.mkdirs(); //mkdir,只新建一個文件夾诸蚕,mkdirs,會將/后面的都新建
}
File file = new File(dir,filename);
if(!file.exists()){
file.createNewFile();
}
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//讀取數(shù)據(jù)
private String read(){
FileInputStream fileInputStream = null;
try {
// fileInputStream = openFileInput(filename);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"peng",filename);
Log.d("path",Environment.getExternalStorageDirectory().getAbsolutePath());
fileInputStream = new FileInputStream(file);
byte[] buff = new byte[1024];
StringBuffer sb = new StringBuffer(); //可實現(xiàn)字符串拼接
int len = 0;
while((len = fileInputStream.read(buff))>0){
sb.append(new String(buff,0,len));
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
{
if(fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return null;
}
}
廣播
activity_broad.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">
<Button
android:id="@+id/btn_intent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳轉(zhuǎn)"/>
<TextView
android:id="@+id/tv_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abx"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize="20sp"/>
</LinearLayout>
activity_broad2.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">
<Button
android:id="@+id/btn_broad1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="intent"
android:textAllCaps="false"/>
</LinearLayout>
代碼:
public class BroadActivity extends AppCompatActivity {
private Button btn_intent;
private TextView tv_test;
private MyBroadcast myBroadcast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broad);
btn_intent = findViewById(R.id.btn_intent);
tv_test = findViewById(R.id.tv_test);
btn_intent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(BroadActivity.this,BroadActivity2.class);
startActivity(intent);
}
});
myBroadcast = new MyBroadcast();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.examle.learn.action");
LocalBroadcastManager.getInstance(this).registerReceiver(myBroadcast,intentFilter);
}
private class MyBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction() != null){
switch (intent.getAction()){
case "com.examle.learn.action":
tv_test.setText("123");
break;
}
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadcast);
}
}
public class BroadActivity2 extends AppCompatActivity {
private Button btn_broad1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broad2);
btn_broad1 = findViewById(R.id.btn_broad1);
btn_broad1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.examle.learn.action");
LocalBroadcastManager.getInstance(BroadActivity2.this).sendBroadcast(intent);
}
});
}
}
屬性動畫
public class ObjectAnimateActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_object_animate);
tv = findViewById(R.id.tv);
//1.
// tv.animate().translationY(500).setDuration(2000).start();
//2.
ValueAnimator valueAnimator = ValueAnimator.ofInt(100,500);
valueAnimator.setDuration(1000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// valueAnimator實際的值
Log.d("animate",animation.getAnimatedValue()+"");
// 動畫的進度0-1
Log.d("animate",animation.getAnimatedFraction()+"");
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.height = (int) animation.getAnimatedValue();
tv.setLayoutParams(params);
tv.setText(animation.getAnimatedValue()+"");
}
});
valueAnimator.start();
//3.
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(tv,"translationY",0,500,200,800);
objectAnimator.setDuration(2000);
objectAnimator.start();
}
}