Add string values into Java | Android Studio

huangapple go评论75阅读模式
英文:

Add string values into Java | Android Studio

问题

public class pomodoroscreen extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {
    TextView dkclsm, yd, txtsaat, tur,dnlm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pomodoroscreen);

        dkclsm = findViewById(R.id.textView2);
        yd = findViewById(R.id.textView4);
        dnlm = findViewById(R.id.textView3);
        txtsaat = findViewById(R.id.txtsaat);

        txtsaat.setText("我想在这里添加字符串,我该怎么做");
    }
}
英文:

I have a string values . How can ı add to text.setText("there") on JAVA Class.

Strings

    <resources>
        <string name="app_name">ViaDo</string>
        <string name="dkdinlenme">minute rest</string>
        <string name="baslat">Start</string>
        <string name="or">or</string>
        <string name="odevhatirlat">Remind Homework</string>
        <string name="dkcalisma">minute study</string>
        <string name="calisma">STUDY</string>
        <string name="hatirlatici">Create a reminder</string>
    </resources>

JAVA Class

 public class pomodoroscreen extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {
    TextView dkclsm, yd, txtsaat, tur,dnlm;
    @Override
            protected void onCreate(Bundle savedInstanceState)   {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.activity_pomodoroscreen);
    dkclsm = findViewById(R.id.textView2);
                yd = findViewById(R.id.textView4);
    dnlm = findViewById(R.id.textView3);
                txtsaat = findViewById(R.id.txtsaat);
    
                 txtsaat.setText("I want add string there How can ı do this");

答案1

得分: 0

你可以通过以下方式实现:

getString(R.string.baslat);

示例:

txtsaat.setText(getString(R.string.baslat));

如果上述方法报错,这里是第二种方法:

this.getResources().getString(R.string.baslat);

其中 'this' 是您的活动(Activity)上下文。

英文:

You can achieve this by:

getString(R.string.baslat);

Example:

txtsaat.setText(getString(R.string.baslat));

If the above one gives you an error here is Method 2:

this.getResources().getString(R.string.baslat);

where 'this' is your Activity Context.

答案2

得分: 0

你可以通过以下方式将 strings.xml 中的字符串传递到 Java 类中。

在 Java 类中:

txtsaat.setText(getString(R.string.app_name));

如果 getString() 出现错误,请尝试通过调用上下文的方式解决。

txtsaat.setText(getApplicationContext().getString(R.string.app_name));

在 Kotlin 中:

applicationContext.getString(R.string.app_name)
英文:

You can get a string from strings.xml to java class in this way.

In Java class

txtsaat.setText(getString(R.string.app_name));

If in case getString() shows an error try this way by calling context.

txtsaat.setText(getApplicationContext().getString(R.string.app_name));

In Kotlin

applicationContext.getString(R.string.app_name)

huangapple
  • 本文由 发表于 2020年9月15日 21:00:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63902393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定