這篇文章給大家介紹SharedPreferences怎么在Android中使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

目前成都創(chuàng)新互聯(lián)公司已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、新賓網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
MainActivity:
public class SharedPreferencesTestActivity extends Activity implements View.OnClickListener{
private EditText editText;
private TextView textView;
private Button write;
private Button read;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences_test);
initView();
write.setOnClickListener(this);
read.setOnClickListener(this);
}
private void initView() {
editText=(EditText)findViewById(R.id.Edit_Test);
textView=(TextView)findViewById(R.id.Text_Test);
write=(Button)findViewById(R.id.write);
read=(Button)findViewById(R.id.read);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.write:
String some=editText.getText().toString();
SharedPreferences pref = SharedPreferencesTestActivity.this.getSharedPreferences("data",MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("Content",some);
editor.commit();
Toast.makeText(SharedPreferencesTestActivity.this, "寫入成功" , Toast.LENGTH_LONG).show();
editText.setText("");
break;
case R.id.read:
SharedPreferences pre = getSharedPreferences("data",MODE_PRIVATE);
String name = pre.getString("Content","");
textView.setText(name);
Toast.makeText(SharedPreferencesTestActivity.this, "讀取成功" , Toast.LENGTH_LONG).show();
break;
}
}
}MainActivity.xml
<LinearLayout 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" android:orientation="vertical" tools:context="com.fae.mobile.testActivity.SharedPreferencesTestActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:textColor="@color/red" android:background="@null" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Edit_Test" android:layout_weight="1" /> <TextView android:textColor="@color/blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Text_Test" android:layout_weight="1"/> </LinearLayout> <Button android:layout_marginTop="25dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/read" android:text="讀"/> <Button android:layout_marginTop="25dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/write" android:text="寫"/> </LinearLayout>
關(guān)于SharedPreferences怎么在Android中使用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
分享標(biāo)題:SharedPreferences怎么在Android中使用
文章位置:http://chinadenli.net/article28/jgjgjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、外貿(mào)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)