|
![](/images/resource/spacer.gif) |
|
Android รบกวนด้วยคร้าบบบบ SharedPreferences ทำงานแล้วแต่ไม่รู้จะใส่ ฟังกชั่นการทำงานอย่างไรดี |
|
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
รบกวนขอความรู้ ครับ เกี่ยวกับ SharedPreferences ผมต้องการทำให้ mMap.setTrafficEnabled(false);เมือไม่มีการเช็ค และเป็น mMap.setTrafficEnabled(true); เมื่อมีการเช็ค ของ Checkbox แต่ใส่เข้าไป ในโค้ดเลยมันไม่ยอมครับ รบกวนด้วยนะครับขอบคุณครับ
โค้ด
Code (Android-Java)
import com.google.android.gms.maps.GoogleMap;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
public class Steting extends Activity {
private CheckBox checkBox;
private TextView textView;
Context context = this;
private Button button;
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_sharepreferrence);
checkBox = (CheckBox) findViewById(R.id.Chk1);
textView = (TextView) findViewById(R.id.Txout);
LoadPrefs();
button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Member();
}
});
}
private void LoadPrefs(){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean cbvalue = sharedPreferences .getBoolean("CHECKBOX", false);
String string = sharedPreferences.getString("Show","Can't Checking");
if (cbvalue){
checkBox.setChecked(true);
}else{
checkBox.setChecked(false);
}
textView.setText(string);
}
private void SavePrefs(String string,boolean value){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sharedPreferences.edit();
editor.putBoolean(string, value);
editor.commit();
}
//////////////////////////////////////////////// Prefer Stage String ///////////////////////////////////////////////////////////////
private void SavePrefs(String string,String value){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sharedPreferences.edit();
editor.putString(string, value);
editor.commit();
}
//////////////////////////////////////////////// Event Stage //////////////////////////////////////////////////////////////////
private void Member(){
final String CK = "Traffic Open";
final String UNCK = "Traffic Closed!!";
SavePrefs("CHECKBOX",checkBox.isChecked());
if(checkBox.isChecked()){
textView.setText(""+CK);
SavePrefs("Show",textView.getText().toString());
Toast.makeText(context, "Traffic Open", Toast.LENGTH_SHORT).show();
}
else{
textView.setText(""+UNCK);
SavePrefs("Show",textView.getText().toString());
Toast.makeText(context, "Traffic Closed!!", Toast.LENGTH_SHORT).show();
}
}
}
Tag : Mobile, Android
![](/images/adv.jpg?v=1001)
|
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2013-07-07 23:56:20 |
By :
029aurora |
View :
1497 |
Reply :
2 |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
ลองดูนี่ครับ
SharedPreferences
ไว้ว่าง ๆ มาต่อเฟส 2 ครับ ![](/images/bbcode/grin.gif?v=1001)
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2013-07-11 17:41:20 |
By :
mr.win |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
ตัวอย่าง UserHelper.java
Code (Java)
public class UserHelper {
Context context;
SharedPreferences sharedPerfs;
Editor editor;
// Prefs Keys
static String perfsName = "UserHelper";
static int perfsMode = 0;
public UserHelper(Context context) {
this.context = context;
this.sharedPerfs = this.context.getSharedPreferences(perfsName, perfsMode);
this.editor = sharedPerfs.edit();
}
public void createSession(String key) {
editor.putBoolean("isLogin", true);
editor.putString("userID", key);
editor.commit();
}
public void deleteSession() {
editor.clear();
editor.commit();
}
public boolean isLogin() {
return sharedPerfs.getBoolean("isLogin", false);
}
public String getUserID() {
return sharedPerfs.getString("userID", null);
}
}
วิธีใช้
Code (Java)
UserHelper usrHelper = new UserHelper(this);
// Create Session
usrHelper.createSession(ID);
// Delete Session
usrHelper.deleteSession();
// Check Login
if (usrHelper.isLogin()) {
} else {
}
// Get UserID
usrHelper.getUserID()
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2014-01-14 19:19:24 |
By :
mr.win |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
|
|
|
![](/images/digitalocean-banner.jpg)
|
Load balance : Server 04
|