xmlइससे पहले कि मैं setTextविजेट्स की तरह कुछ और करूं, मैं एक फाइल से स्ट्रिंग पढ़ना चाहता हूं , इसलिए मैं कॉल करने के लिए गतिविधि ऑब्जेक्ट के बिना ऐसा कैसे कर सकता हूं getResources()?
xmlइससे पहले कि मैं setTextविजेट्स की तरह कुछ और करूं, मैं एक फाइल से स्ट्रिंग पढ़ना चाहता हूं , इसलिए मैं कॉल करने के लिए गतिविधि ऑब्जेक्ट के बिना ऐसा कैसे कर सकता हूं getResources()?
जवाबों:
Applicationउदाहरण के लिए, का उपवर्ग बनाएँpublic class App extends Application {android:nameअपने <application>टैग की विशेषता सेट करें AndroidManifest.xml, जैसेandroid:name=".App"onCreate()अपने एप्लिकेशन इंस्टेंस की विधि में, अपने संदर्भ (उदा this) नाम के एक स्थिर फ़ील्ड को सहेजें mContextऔर एक स्थिर विधि बनाएं जो इस फ़ील्ड को लौटाता है, जैसे getContext():यह इस तरह दिखना चाहिए:
public class App extends Application{
private static Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = this;
}
public static Context getContext(){
return mContext;
}
}
अब आप उपयोग कर सकते हैं: App.getContext()जब भी आप एक संदर्भ प्राप्त करना चाहते हैं, और तब getResources()(या App.getContext().getResources())।
केवल सिस्टम संसाधनों के लिए!
उपयोग
Resources.getSystem().getString(android.R.string.cancel)
आप स्थैतिक स्थिरांक घोषणाओं में भी उन्हें अपने आवेदन में हर जगह उपयोग कर सकते हैं!
Toastउदाहरण के लिए, SharedPreferenceउदाहरण के लिए, डेटाबेस खोलना, जैसा कि मेरी लैटिन भाषा के शिक्षक कहते हैं: et cetera )।
मेरा Kotlin समाधान एक स्थिर अनुप्रयोग संदर्भ का उपयोग करना है:
class App : Application() {
companion object {
lateinit var instance: App private set
}
override fun onCreate() {
super.onCreate()
instance = this
}
}
और स्ट्रिंग्स वर्ग, जिसका मैं हर जगह उपयोग करता हूं:
object Strings {
fun get(@StringRes stringRes: Int, vararg formatArgs: Any = emptyArray()): String {
return App.instance.getString(stringRes, *formatArgs)
}
}
तो आपके पास संसाधन स्ट्रिंग्स प्राप्त करने का एक साफ तरीका हो सकता है
Strings.get(R.string.some_string)
Strings.get(R.string.some_string_with_arguments, "Some argument")
कृपया इस उत्तर को न हटाएं, मुझे एक रखने दें।
Stringsमददगार था।
एक और आधिपत्य भी है। मैं इस तरह से संसाधनों से OpenGl shaders लोड करता हूं:
static private String vertexShaderCode;
static private String fragmentShaderCode;
static {
vertexShaderCode = readResourceAsString("/res/raw/vertex_shader.glsl");
fragmentShaderCode = readResourceAsString("/res/raw/fragment_shader.glsl");
}
private static String readResourceAsString(String path) {
Exception innerException;
Class<? extends FloorPlanRenderer> aClass = FloorPlanRenderer.class;
InputStream inputStream = aClass.getResourceAsStream(path);
byte[] bytes;
try {
bytes = new byte[inputStream.available()];
inputStream.read(bytes);
return new String(bytes);
} catch (IOException e) {
e.printStackTrace();
innerException = e;
}
throw new RuntimeException("Cannot load shader code from resources.", innerException);
}
जैसा कि आप देख सकते हैं, आप पथ में किसी भी संसाधन को अपनी कक्षा में /res/...
बदल aClassसकते हैं। यह भी कि मैं परीक्षण में संसाधनों को कैसे लोड करता हूं (androidTests)
सिंगलटन:
package com.domain.packagename;
import android.content.Context;
/**
* Created by Versa on 10.09.15.
*/
public class ApplicationContextSingleton {
private static PrefsContextSingleton mInstance;
private Context context;
public static ApplicationContextSingleton getInstance() {
if (mInstance == null) mInstance = getSync();
return mInstance;
}
private static synchronized ApplicationContextSingleton getSync() {
if (mInstance == null) mInstance = new PrefsContextSingleton();
return mInstance;
}
public void initialize(Context context) {
this.context = context;
}
public Context getApplicationContext() {
return context;
}
}
अपने Applicationउपवर्ग में सिंगलटन की शुरुआत करें :
package com.domain.packagename;
import android.app.Application;
/**
* Created by Versa on 25.08.15.
*/
public class mApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ApplicationContextSingleton.getInstance().initialize(this);
}
}
अगर मैं गलत नहीं हूँ, तो यह आपको हर जगह ApplicationContext के लिए एक हुक देता है, इसे ApplicationContextSingleton.getInstance.getApplicationContext();
आप के साथ कॉल करें किसी भी बिंदु पर आपको इसे खाली करने की आवश्यकता नहीं है, जैसे ही अनुप्रयोग बंद हो जाता है, वैसे भी इसके साथ जाता है।
AndroidManifest.xmlइस Applicationउपवर्ग का उपयोग करने के लिए अपडेट करना याद रखें :
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.packagename"
>
<application
android:allowBackup="true"
android:name=".mApplication" <!-- This is the important line -->
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:icon="@drawable/app_icon"
>
अब आप ApplicationContextSingleton.getInstance () .AppApplicationContext ()। GetResources () का उपयोग कहीं से भी कर सकते हैं, वह भी बहुत कम जगहों पर जहां एप्लिकेशन उपवर्ग can´t।
कृपया मुझे बताएं कि अगर आपको यहां कुछ भी गलत दिखाई देता है, तो धन्यवाद। :)
एक और समाधान:
यदि आपके पास एक गैर-स्थिर बाहरी वर्ग में एक स्थिर उपवर्ग है, तो आप बाहरी वर्ग में स्थैतिक चर के माध्यम से उपवर्ग के भीतर से संसाधनों तक पहुंच सकते हैं, जिसे आप बाहरी वर्ग के निर्माण पर आरंभ करते हैं। पसंद
public class Outerclass {
static String resource1
public onCreate() {
resource1 = getString(R.string.text);
}
public static class Innerclass {
public StringGetter (int num) {
return resource1;
}
}
}
मैंने इसे अपने FragmentActivity के भीतर स्थिर FragmentPagerAdapter के GetPageTitle (int स्थिति) फ़ंक्शन के लिए उपयोग किया, जो I8N के कारण उपयोगी है।
मैं App.getRes()इसके बजाय उपयोग App.getContext().getResources()करता हूं (जैसा कि @Cristian ने उत्तर दिया)
अपने कोड में कहीं भी उपयोग करना बहुत सरल है!
तो यहाँ एक अनूठा समाधान है जिसके द्वारा आप संसाधनों को कहीं से भी एक्सेस कर सकते हैं Util class।
(१) अपनी Applicationकक्षा बनाएं या संपादित करें ।
import android.app.Application;
import android.content.res.Resources;
public class App extends Application {
private static App mInstance;
private static Resources res;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
res = getResources();
}
public static App getInstance() {
return mInstance;
}
public static Resources getResourses() {
return res;
}
}
(2) अपने manifest.xml <applicationटैग में नाम फ़ील्ड जोड़ें । (या इसे छोड़ें यदि पहले से ही है)
<application
android:name=".App"
...
>
...
</application>
अब आप जाने के लिए अच्छे हैं।
App.getRes().getString(R.string.some_id)कोड में कहीं भी उपयोग करें ।मुझे लगता है, अधिक रास्ता संभव है। लेकिन कभी-कभी, मैं इस समाधान का उपयोग कर रहा हूं। (पूर्ण वैश्विक):
import android.content.Context;
import <your package>.R;
public class XmlVar {
private XmlVar() {
}
private static String _write_success;
public static String write_success() {
return _write_success;
}
public static void Init(Context c) {
_write_success = c.getResources().getString(R.string.write_success);
}
}
//After activity created:
cont = this.getApplicationContext();
XmlVar.Init(cont);
//And use everywhere
XmlVar.write_success();
मैं स्थिर कार्य से ओपन ES के लिए shader लोड करता हूं।
याद रखें कि आपको अपनी फ़ाइल और निर्देशिका नाम के लिए निचले मामले का उपयोग करना होगा, अन्यथा ऑपरेशन विफल हो जाएगा
public class MyGLRenderer implements GLSurfaceView.Renderer {
...
public static int loadShader() {
// Read file as input stream
InputStream inputStream = MyGLRenderer.class.getResourceAsStream("/res/raw/vertex_shader.txt");
// Convert input stream to string
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
String shaderCode = s.hasNext() ? s.next() : "";
}
...
}
public Static Resources mResources;
@Override
public void onCreate()
{
mResources = getResources();
}
मैं एपीआई स्तर 27 का उपयोग कर रहा हूं और लगभग दो दिनों तक संघर्ष करने के बाद सबसे अच्छा समाधान पाया। यदि आप एक वर्ग से एक xml फ़ाइल पढ़ना चाहते हैं जो गतिविधि या अनुप्रयोग से नहीं निकलती है तो निम्न कार्य करें।
संपत्ति निर्देशिका के अंदर testdata.xml फ़ाइल रखें।
निम्नलिखित कोड को टेस्टडेटा दस्तावेज़ को पार्स करने के लिए लिखें।
InputStream inputStream = this.getClass().getResourceAsStream("/assets/testdata.xml");
// create a new DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// use the factory to create a documentbuilder
DocumentBuilder builder = factory.newDocumentBuilder();
// create a new document from input stream
Document doc = builder.parse(inputStream);अपनी कक्षा में, जहाँ आप स्थैतिक कार्य को लागू करते हैं, आप इस कक्षा से एक निजी \ सार्वजनिक विधि को कॉल कर सकते हैं । निजी \ सार्वजनिक विधि getResources का उपयोग कर सकती है ।
उदाहरण के लिए:
public class Text {
public static void setColor(EditText et) {
et.resetColor(); // it works
// ERROR
et.setTextColor(getResources().getColor(R.color.Black)); // ERROR
}
// set the color to be black when reset
private void resetColor() {
setTextColor(getResources().getColor(R.color.Black));
}
}
और अन्य वर्ग की गतिविधि से, आप कॉल कर सकते हैं:
Text.setColor('some EditText you initialized');
यदि आपके पास एक संदर्भ है, तो मेरा मतलब है अंदर;
public void onReceive(Context context, Intent intent){
}
संसाधन प्राप्त करने के लिए आप इस कोड का उपयोग कर सकते हैं:
context.getResources().getString(R.string.app_name);