2017-02-23 79 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());