从 prefences.xml 中放入一些关键文本 [英] put some key text from prefences.xml

查看:29
本文介绍了从 prefences.xml 中放入一些关键文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的首选项.xml 中保存了一些数据但我想把 url 放到我的 Activity.java 中

i have a save some data from my preferences.xml but i want to put url to my activitity.java

这里是我的代码;public static final String PREFS_NAME = "mypreferemce.xml";

这是我从preference.xml中获取一些密钥的网址

and this is my url to get some key from preference.xml

 public void lampu1(View view) {
    boolean on = ((ToggleButton) view).isChecked();
    if (on) {

        final WebView wv = (WebView) findViewById(R.id.webview5);
        wv.setWebViewClient(new WebViewClient(){
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                wv.loadUrl( "file:///android_asset/1.html");
                Toast.makeText(getApplicationContext(),
                        "koneksi error", Toast.LENGTH_LONG).show();
            }
        });
        wv.canGoBack();
       wv.loadUrl("http://someurl.com");


    }

这是我的首选项.xml

and this is my preference.xml

<string name="mypreference_string">http://someurl.com</string>

感谢帮助

推荐答案

使用下面的代码创建一个这样的类,并且在您需要使用共享首选项保存任何内容的任何地方使用它的set 方法并从 SharedPreference 检索任何值,使用其 get 方法.

Use below code make a class like this and wherever you need to use to save any thing in shared preference use its set method and to retrieve any value from SharedPreference use its get method.

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharedPrefManager {

    public static SharedPreferences getSharedPref(Context mContext) {
        SharedPreferences pref = mContext.getSharedPreferences(Constants.SETTINGS, Context.MODE_PRIVATE);

        return pref;
    }

    public static void setPrefVal(Context mContext, String key, String value) {
        if(key!=null){
        Editor edit = getSharedPref(mContext).edit();
        edit.putString(key, value);
        edit.commit();
        }
    }

    public static void setIntPrefVal(Context mContext, String key, int value) {
        if(key!=null){
            Editor edit = getSharedPref(mContext).edit();
            edit.putInt(key, value);
            edit.commit();
        }
    }

    public static void setLongPrefVal(Context mContext, String key, Long value) {
        if(key!=null){
            Editor edit = getSharedPref(mContext).edit();
            edit.putLong(key, value);
            edit.commit();
        }
    }

    public static void setBooleanPrefVal(Context mContext, String key, boolean value) {
        if(key!=null){
            Editor edit = getSharedPref(mContext).edit();
            edit.putBoolean(key, value);
            edit.commit();
        }
    }


    public static String getPrefVal(Context mContext, String key) {
        SharedPreferences pref = getSharedPref(mContext);
        String val = "";
        try {
            if (pref.contains(key))
                val = pref.getString(key, "");
            else
                val = "";
        }catch (Exception e){
            e.printStackTrace();
        }
        return val;
    }

    public static int getIntPrefVal(Context mContext, String key) {
        SharedPreferences pref = getSharedPref(mContext);
        int val = 0;
        try {
        if(pref.contains(key)) val = pref.getInt(key, 0);
        }catch (Exception e){
            e.printStackTrace();
        }
        return val;
    }

    public static Long getLongPrefVal(Context mContext, String key) {
        SharedPreferences pref = getSharedPref(mContext);
        Long val = null;
        try{
        if(pref.contains(key)) val = pref.getLong(key, 0);
    }catch (Exception e){
        e.printStackTrace();
    }
        return val;
    }

    public static boolean getBooleanPrefVal(Context mContext, String key) {
        SharedPreferences pref = getSharedPref(mContext);
        boolean val = false;
        try{
        if(pref.contains(key)) val = pref.getBoolean(key, false);

        }catch (Exception e){
            e.printStackTrace();
        }
        return val;
    }


    public static boolean containkey(Context mContext,String key)
    {
        SharedPreferences pref = getSharedPref(mContext);
        return pref.contains(key);
    }   

}

如果需要任何帮助来实现这一点,请尝试一下,然后告诉我.

Try it if any help is needed to implement this then let me know.

这篇关于从 prefences.xml 中放入一些关键文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆