2016-03-17 86 views
1

有人可以幫我編輯下面的代碼,以便它將輸入的信息保存到edittext中,然後一旦應用程序重新啓動,它將自動顯示保存在edittext字段中的文本。我已經嘗試過SharedPreferences教程,但到目前爲止我還無法使它工作。Android Edittext Sharedpreferences

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.text.TextUtils; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 


public class MainActivity extends Activity { 

EditText txtLink; 
Button btnOpenLink; 
String defaultLink; 
String secondLink; 
public static final String MyPREFERENCES = "MyPrefs" ; 
public static final String Name = "nameKey"; 
SharedPreferences sharedpreferences; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

defaultLink = "http://"; 
secondLink = ".whatver.com"; 

txtLink = (EditText) findViewById(R.id.editText); 
btnOpenLink = (Button) findViewById(R.id.button); 
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
btnOpenLink.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View arg0) { 
    String server = txtLink.getText().toString(); 
    SharedPreferences.Editor editor = sharedpreferences.edit(); 
    editor.putString(Name, server); 
    editor.commit(); 
    if(!TextUtils.isEmpty(server)){ 
     Intent intent=new Intent(MainActivity.this,webactivity.class); 
     intent.setData(Uri.parse(defaultLink+server+secondLink)); 
     startActivity(intent); 
    }else{ 
     Toast.makeText(MainActivity.this, "Please enter your server name.", Toast.LENGTH_LONG).show(); 
    } 
} 
}); 
} 

} 
+0

請粘貼您嘗試使用的首選項代碼。 –

+0

我嘗試了本教程中的所有內容http://www.tutorialspoint.com/android/android_shared_preferences.htm – carnick

+0

無論您嘗試過什麼,請粘貼它。 –

回答

0

我通過調用這個來修復它

sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
    txtLink.setText(sharedpreferences.getString(Name, "")); 
0

驗證存儲的偏好,如果任何avaliable加載它們,之後在TextView中顯示這些

例子:

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

defaultLink = "http://"; 
secondLink = ".whatver.com"; 

txtLink = (EditText) findViewById(R.id.editText); 
btnOpenLink = (Button) findViewById(R.id.button); 

checkIfPreferencesAvaliable(); 

if(defaultLink!=null && secondLink!=null){ 
    txtLink.setText(defaultLink+secondLink); 
} 

defaultLink = "http://"; 
secondLink = ".whatver.com"; 

void checkIfPreferencesAvaliable(){ 
    SharedPreferences preferences = getPreferences(Activity.MODE_PRIVATE); 
    defaultLink = preferences.getStr("mydefaultLink", null); 
    secondLink = preferences.getStr("mysecondLink", null); 
} 
0

在你onCreate(),請檢查您是否已經有值您的偏好 -

String savedVal = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE).getString(Name, null); 

if(savedVal == null){ //This means you don't have the saved values in your prefs file 
//Do whatever you want to 
} else{ 
//Use "savedVal" anyway you want 
} 
+0

我對此很陌生,你能告訴我該放哪裏以及如何加載保存的字符串? – carnick

+0

在你的onCreate()方法粘貼我提供的代碼,如果你想在TextView中設置它,你可以使用「savedVal」來設置它。 –

+0

我想什麼它做的是 如果(savedVal == NULL){無能爲力 }其他{ 顯示它在EDITTEXT領域 } 我怎樣才能做到這一點? – carnick