Skip to content

Instantly share code, notes, and snippets.

@saitou-k
Created October 11, 2018 09:38
Show Gist options
  • Save saitou-k/8195258234a205e6ed983cf258037fd5 to your computer and use it in GitHub Desktop.
Save saitou-k/8195258234a205e6ed983cf258037fd5 to your computer and use it in GitHub Desktop.
アンドロイドでリソースファイルを使う(MainActivity.java)
package com.sample.android_resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getTextで文字列を取得
String str1 = getString(R.string.sample1);
String str2 = getString(R.string.sample2,"りんご",3);
// ログ出力
Log.d("sample","str1:"+str1);
Log.d("sample","str2:"+str2);
// ビューにも反映
TextView text1 = (TextView)findViewById(R.id.textView1);
text1.setText(str1);
TextView text2 = (TextView)findViewById(R.id.textView2);
text2.setText(str2);
// コードからマークアップを反映させる
// TextView text3 = (TextView)findViewById(R.id.textView1);
// text3.setText(getText(R.string.sample4));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment