從數(shù)據(jù)存儲到屬性動畫

數(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();
}
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末倔矾,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子丰包,更是在濱河造成了極大的恐慌提陶,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件锌蓄,死亡現(xiàn)場離奇詭異撑柔,居然都是意外死亡,警方通過查閱死者的電腦和手機剪决,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進店門柑潦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來峻凫,“玉大人,你說我怎么就攤上這事荧琼∶” “怎么了?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵镐侯,是天一觀的道長驶冒。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么怜俐? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任拍鲤,我火速辦了婚禮汞扎,結(jié)果婚禮上擅这,老公的妹妹穿的比我還像新娘。我一直安慰自己痹扇,他們只是感情好溯香,可當(dāng)我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布玫坛。 她就那樣靜靜地躺著,像睡著了一般炕吸。 火紅的嫁衣襯著肌膚如雪勉痴。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天嘴瓤,我揣著相機與錄音莉钙,去河邊找鬼。 笑死停忿,一個胖子當(dāng)著我的面吹牛蚊伞,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播颅停,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼掠拳,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了喊熟?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤烦味,失蹤者是張志新(化名)和其女友劉穎谬俄,沒想到半個月后扇商,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡蔬芥,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年笔诵,在試婚紗的時候發(fā)現(xiàn)自己被綠了姑子。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡谢翎,死狀恐怖沐旨,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情磁携,我是刑警寧澤谊迄,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站歪脏,受9級特大地震影響粮呢,放射性物質(zhì)發(fā)生泄漏怠硼。R本人自食惡果不足惜移怯,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一舟误、第九天 我趴在偏房一處隱蔽的房頂上張望姻乓。 院中可真熱鬧,春花似錦赖草、人聲如沸剪个。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽侵歇。三九已至,卻和暖如春坟冲,著一層夾襖步出監(jiān)牢的瞬間溃蔫,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工矩桂, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留痪伦,地道東北人。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓癞蚕,卻偏偏與公主長得像辉哥,于是被迫代替她去往敵國和親攒射。 傳聞我的和親對象是個殘疾皇子恒水,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容