Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android Studio的xml中使用java中的变量?_Java_Android_Xml - Fatal编程技术网

如何在Android Studio的xml中使用java中的变量?

如何在Android Studio的xml中使用java中的变量?,java,android,xml,Java,Android,Xml,我正在尝试使用TextView制作一个记分计数器。我已将文本设置为“0”,并希望它在每次按下按钮时增加。我怎样才能做到这一点 到目前为止,我试图从活动的java中调用变量,但我不知道如何从xml中调用变量。或者从java更改xml的值 如有任何帮助,我们将不胜感激 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android

我正在尝试使用TextView制作一个记分计数器。我已将文本设置为“0”,并希望它在每次按下按钮时增加。我怎样才能做到这一点

到目前为止,我试图从活动的java中调用变量,但我不知道如何从xml中调用变量。或者从java更改xml的值

如有任何帮助,我们将不胜感激

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="0"
        android:id="@+id/score"
        android:textSize="30sp"
        android:textColor="@color/black_overlay"
        android:layout_below="@+id/adView"
        android:layout_centerHorizontal="true" />
像这样试试

public void buttonOnClick (View view) {
   Toast.makeText(getApplicationContext(), "Click", Toast.LENGTH_SHORT).show();
   TextView score = (TextView) findViewById(R.id.score);
   score.setText("your_score");
}

您可以尝试以下方法:

int count = getResources().getInteger(R.integer.my_count);//why use string if you can use int
textView.setText(""+count++);//"" will cast count to string

严格地说,不能用XML存储变量。但是,您可以只获取字段中的值,增加它,然后存储新值:

TextView view = (TextView) getViewById(R.id.score);
int score = Integer.parseInt(view.getText().toString());
view.setText(""+score++);

将此内容放入onClick方法。

请先阅读。稍后,您必须使用
textView.getText()
textView.setText()
来更新它。谢谢!非常好,非常感谢你的帮助。
public void buttonOnClick (View view) {
    Toast.makeText(getApplicationContext(), "Click", Toast.LENGTH_SHORT).show();
    //String yourString = this.getResources().getString(R.string.scoreCount);

    String mystring = getResources().getString(R.string.scoreCount);

}