2017-02-23 80 views
-5

我有一个edittext和按钮,它将editText文本传递给其他活动onClick当涉及到第二个活动时,它从主活动接收文本。如何动态设置字符串资源

我的问题是,我在字符串资源文件夹中有超过20个字符串名称,我必须打开与主要活动的edittext值匹配的特定字符串。 总之,我必须要改变它相匹配的字符串资源字符串

<resources> 
     <string name="app_name">sylb</string> 
     <string name="title_activity_display">display</string> 
     <string name="large_text">some_large_text</string> 
     <string name="ds15mca21">second mca</string> 
     <string name="ds15mca11">first mca</string> 
     <string name="ds15mba21">second <b>mba</b>></string> 
     <string name="ds15aut21">second <i>automobile</i></string> 
     <string name="action_settings">Settings</string> 
</resources> 

显示代码

public class display extends AppCompatActivity { 
     String code; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_display); 
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 
      Intent intent = getIntent(); 
      Bundle extras = intent.getExtras(); 
      code= extras.getString("keyName"); 
      TextView textView= (TextView) findViewById(R.id.textapp); 

      textView.setText("R.string." + "ds" + code); 
     } 
    } 
+0

textView.setText( “R.string。” + “DS” +代码); 这行显示正常文本为r.string.ds <检索到的代码> –

+1

如果您想从“字符串”文件设置字符串值,您应该使用我的答案!那么,究竟想要做什么?请详细解释一下! –

+0

而不是将'code'作为额外的字符串参数传递。传递'R.string.dsXYZ'的值作为额外的int资源。然后在你的其他活动中获取该int并使用'textView.setText(resInt);' –

回答

0

使用下面的代码的textview的文字!

getResources().getString(resId)//resId is R.string.STRING_NAME 
+0

@AsutoshPanda不理解你!请解释更多 –

0

你可以试试这个

textView.setText(getResources().getIdentifier("R.string." + "ds" + code,"string",getPackageName())); 
+4

关于堆栈溢出,通常会添加几个字的解释,而不是一行代码... – mkl

1

这将做你的工作! 只是通过你固定的,而不是“app_name”正确String(不要使用R.string只是资源名称)

例子:如果我想要得到的

<string name="app_name">ItsCharuHereHiFolks</string> 

你需要的价值为此

final TextView textView = (TextView) findViewById(R.id.your_text_view); 
String string = getString(getResId("app_name", R.string.class)); 
textView.setText(string); 

public static int getResId(String resName, Class<?> c) { 

     try { 
      Field idField = c.getDeclaredField(resName); 
      return idField.getInt(idField); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return -1; 
     } 
    } 
+0

因此,你建议他传递字符串资源的名称,然后从中计算字符串ID,然后再次获取实际字符串的值,然后将其分配给textView?当真?传递属于该字符串资源的实际字符串或直接将resId作为与该字符串资源对应的额外参数传递。 –

+0

@Mohammed Atif他的要求是使用字符串资源的名称获取字符串值。阅读问题,那就是他想用“R.string”来做什么。 +“ds” –

+0

他是一名初学者,他可能不知道做更简单的任务的更好方法。这并不意味着你会回答他的问题,而不是帮助他找到更好的解决方案。 –

0

它真的很容易。在类

private String getStringResourceByName(String aString) { 
     String packageName = getPackageName(); 
     int resId = getResources().getIdentifier(aString, "string", packageName); 
     return getString(resId); 
    } 

使用,只需使用该

textView.setText(getStringResourceByName("ds" + code)); 

或者你可以简单地使用上面的类和提取任何字符串。

  • 如果你从下面的资源

    <string name="questions_en">Questions</string>

使用此

String question=getStringResourceByName("questions_en"); 
questions_en

另外用整数代替字符串getStringResourceByName 获取整数资源的方法。

getIdentifier (String name, String defType, String defPackage)做的工作!

Credits

0

尽管@ Charu的答案很好,而且足够高效,可以执行其中id属于不同资源类型的通用任务,但如果知道字符串资源名称并且您的任务仅与Strings相关,则仍然可以获得更好的解决方案。

第1步

在您电话的活动

,使用下面的代码。

Intent intent = new Intent(this, newActivity.class); 
intent.putExtra("StringResourceId", R.string.dsAbcXyz); //this step will directly pass the int value of your string resource without any iterations 
startActivity(intent); 

步骤2

然后在你的newActivity,使用下面的代码。当你很肯定的resourxe名传递作为参数

Intent prevIntent = getIntent(); 
int resId = prevIntent.getIntExtra("StringResourceId"); 
textView.setText(resId); //you can directly pass the int id to textview to display the relevant text 

这种方法可以工作。

但是,如果您通过随机来源(如服务器 或数据库)获得您的资源名称,那么我建议您遵循@ Charu的解决方案。

-1

使用本

getResources().getIdentifier("ds" + code, "String", getPackageName());