5

私はstrings.xmlで宣言された次の文字列を持っています:

<string name="last_msg">Your last click was on</string>

誰かがボタンをクリックすると、テキストビューにこの文字列をスペースで表示し、次にタイムスタンプである変数値を表示する必要があります。

残念ながら、@string/last_msg を使用しても機能しません。これを適切に行う方法がわからないため、コンテンツをハードコーディングしていません。

onClick 関数のコードは次のとおりです。

public void showMsgNow(View view) {
    TextView lastMsg = (TextView)findViewById(R.id.textView2);
    long currentTimeStamp = System.currentTimeMillis();
    lastMsg.setText(@string/last_msg + " " + currentTimeStamp);
}

私は初心者です。

4

5 に答える 5

11

Googleで答えを見つけました:

getString(R.string.last_msg)
于 2012-05-22T09:08:26.810 に答える
10

Stringで直接アクセスする@ことはできません。そのため、コンテキストリソースが必要であり、これを行うだけです...

lastMsg.setText(context.getResources().getString(R.string.last_msg) + " " + currentTimeStamp);

あなたの場合、使用

<string name="last_msg">Your last click was on %1$s</string>

実装:

public void showMsgNow(View view) {
    TextView lastMsg = (TextView)findViewById(R.id.textView2);
    long currentTimeStamp = System.currentTimeMillis();
    lastMsg.setText(context.getResources()
        .getString(R.string.last_msg, currentTimeStamp));
}
于 2012-05-22T09:09:13.673 に答える
6
// getString is method of context
if (this instanceof Context) 
//If you are in Activity or Service class            
 lastMsg.setText(getString(R.string.last_msg)+ " " + currentTimeStamp);
else                         
//you need to context to get the string 
  lastMsg.setText(getString(mContext,R.string.last_msg)+ " " + currentTimeStamp);


  public String getString(Context mContext, int id){
     return mContext.getResources().getString(id);
  }
于 2012-05-22T09:08:14.440 に答える
1

下の行を使用

 lastMsg.setText(getString(R.string.last_msg) + " " + currentTimeStamp);
于 2012-05-22T09:09:38.523 に答える
0

これを試して :

lastMsg.setText(R.string.last_msg + " " + new SimpleDateFormat(d-MM-YYYY).format(new Date()));
于 2012-05-22T09:13:23.203 に答える