2017-02-04 4 views
0

私のコードでは、TextViewがクリックされた場合、javaファイル内の別の文字列(string.xml)に置き換えたいxml文字列があります。 string.xmlファイルの文字列をmainactity.javaファイルに入力できますか?MainActivity.javaのTextViewのtext.xmlをstring.xmlで設定することはできますか?

//activity_main.xml 
android:id="@+id/description" 
android:text="description of activity" 

//MainActivity.java 
TextView moreInfo = (TextView) findViewById(R.id.description); 
    moreInfo.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      moreInfo.setText(@string/description_activity_main);) 
     } 
    }); 

答えて

0

はい、可能です。あなたはそれを間違っているだけです。ここではあなたが何をすべきかです:RES /値/のstrings.xmlに保存

moreInfo.setText(getString(R.string.description_activity_main)); 
0

XMLファイル:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="hello">Hello!</string> 
</resources> 

このアプリケーションコードは、文字列を取得します。

String string = getString(R.string.hello); 
0

あなたを使用することができます文字列string.xmlからRファイルをインポート後に使用できるgetResources()を使用して、

import com.example.R; // com.example should be replaced your package path 

そしてgetString()機能

moreInfo.setText(getResources().getString(R.string.description_activity_main)); 
0

使用、以下のように変更

getString(R.string.sting_name)
関連する問題