Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.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
Java 如何显示变量的值?-安卓_Java_Android_Variables_Textview_Show - Fatal编程技术网

Java 如何显示变量的值?-安卓

Java 如何显示变量的值?-安卓,java,android,variables,textview,show,Java,Android,Variables,Textview,Show,我想在文本视图中显示变量的值,就像php中的echo一样 我会解释的。。我有一个名为“contadorRegistros”的变量,用于存储应用程序的记录数。。我想在文本视图中显示它的值 在php中,我会这样做: <php echo "this is my variable $contadorRegistros" ?> 但当我按所需变量切换“Test”时,它不起作用: TextView t = (TextView) findViewById (R.id.tvCont); t.se

我想在文本视图中显示变量的值,就像php中的echo一样

我会解释的。。我有一个名为“contadorRegistros”的变量,用于存储应用程序的记录数。。我想在文本视图中显示它的值

在php中,我会这样做:

<php

echo "this is my variable $contadorRegistros"

?>
但当我按所需变量切换“Test”时,它不起作用:

TextView t = (TextView) findViewById (R.id.tvCont);
t.setText (contadorRegistros);


我能为工作做些什么=/

您希望在java代码中包含以下内容:

t=new TextView(this);

t=(TextView)findViewById(R.id.TextView1); 
t.setText("I changed the text");
必须按照您命名textview的方式更改R.id。此外,只需创建一个字符串并将变量名添加到该字符串中。然后让setText显示它

编辑:
另请参见

如果变量是int,则执行类似操作会有所帮助

t.setText (contadorRegistros+"");

数字没有显示,因为它是一个整数,setText()方法将String或CharSequence作为其参数。 因此,您应该首先将数字转换为字符串。只有这样,您才能在setText()方法中使用它。您可以这样做:

   t.setText(Integer.toString(contadorRegistros));

你读过安卓系统吗?有趣的是,一个直接的答案被选为正确答案,而我倾向于给出一个答案,让他自己做一些编码——为了学习——却被忽略了。问题解决了!(有人可能会质疑,在这段代码中t是什么)
   t.setText(Integer.toString(contadorRegistros));