使用 1 个文本视图显示文本列表
Display list of text using 1 textview
我正在尝试在同一个文本视图中显示结果列表。
下面是我的代码...此代码有效但仅显示 for loop.I 的最后一个值需要代码来打印以下内容。
The result is : 2 * 0 = 0
The result is : 2 * 1 = 2
The result is : 2 * 2 = 4
The result is : 2 * 3 = 6
The result is : 2 * 4 = 8
The result is : 2 * 5 = 10
The result is : 2 * 6 = 12
The result is : 2 * 7 = 14
The result is : 2 * 8 = 16
The result is : 2 * 9 = 18
请指教。提前致谢。
@Override
public void onClick(View v) {
if (v.getId() == R.id.submit){
for (int i = 0; i < 10; i++) {
int userInput, result;
userInput = Integer.parseInt(user.getText().toString());
result = userInput * i;
ans.setText("The result is : " + userInput + " * " + i + " = " + result);
System.out.println("The result is : " + userInput + " * " + i + " = " + result);
}
}
}
在textview中,只会最后设置textview中的文本。
设置文本,当 i=0 时,然后在 for 循环增量之后将文本追加到 textview 中。
之后你就会得到想要的结果。
if(i==0)
ans.setText("The result is : " + userInput + " * " + i + " = " + result);
else
ans.append("\nThe result is : " + userInput + " * " + i + " = " + result);
您可以使用 String
变量通过 concat
函数显示所有结果,在执行循环后只需将此 string
设置为您的 textView
:
String answerString = "";
@Override
public void onClick(View v) {
if (v.getId() == R.id.submit){
for (int i = 0; i < 10; i++)
{
int userInput, result;
userInput = Integer.parseInt(user.getText().toString());
result = userInput * i;
answerString = answerString.concat("The result is : " + userInput + " * " + i + " = " + result);
answerString = answerString.concat("\n");
}
ans.setText(answerString);
}
}
您也可以在循环前只使用一次 "The result is" 并显示结果,如:
String answerString = "";
@Override
public void onClick(View v) {
if (v.getId() == R.id.submit){
answerString = "The result is : \n";
for (int i = 0; i < 10; i++)
{
int userInput, result;
userInput = Integer.parseInt(user.getText().toString());
result = userInput * i;
answerString = answerString.concat(userInput + " * " + i + " = " + result);
answerString = answerString.concat("\n");
}
ans.setText(answerString);
}
}
你的输出将是这样的:
The result is :
2 * 0 = 0
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
替换
ans.setText("The result is : " + userInput + " * " + i + " = " + result);
和
ans.append("The result is : " + userInput + " * " + i + " = " + result + "\n");
你的问题就解决了
您可以使用 append
而不是 setText
示例:
ans.append("The result is : " + userInput + " * " + i + " = " + result + "\n");
或
您可以使用 String 变量使用 +=
连接字符串值,最后在循环 setText
之后到 TextView
示例:
String strMultiplication = ""; // declare string before for loop
for (int i = 0; i < 10; i++) {
int userInput, result;
userInput = Integer.parseInt(user.getText().toString());
result = userInput * i;
strMultiplication += "The result is : " + userInput + " * " + i + " = " + result +"\n"); //String concatenation
System.out.println("The result is : " + userInput + " * " + i + " = " + result);
}
ans.setText(strMultiplication); // setText To the textView outside the loop
package com.example.count;
import java.util.Scanner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button b1;
TextView tv;
int input=2;
int result;
String finalresult="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.txt);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String res= rint();
tv.setText(res);
}
});
}
public String rint(){
for (int i = 0; i < 10; i++) {
result = input * i;
finalresult=finalresult+"\n"+"The result is : " +input + " * " + i + " = " + result;
}
return finalresult;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="14dp"
android:inputType="textMultiLine"
android:text="@string/hello_world" />
</RelativeLayout>
我正在尝试在同一个文本视图中显示结果列表。
下面是我的代码...此代码有效但仅显示 for loop.I 的最后一个值需要代码来打印以下内容。
The result is : 2 * 0 = 0
The result is : 2 * 1 = 2
The result is : 2 * 2 = 4
The result is : 2 * 3 = 6
The result is : 2 * 4 = 8
The result is : 2 * 5 = 10
The result is : 2 * 6 = 12
The result is : 2 * 7 = 14
The result is : 2 * 8 = 16
The result is : 2 * 9 = 18
请指教。提前致谢。
@Override
public void onClick(View v) {
if (v.getId() == R.id.submit){
for (int i = 0; i < 10; i++) {
int userInput, result;
userInput = Integer.parseInt(user.getText().toString());
result = userInput * i;
ans.setText("The result is : " + userInput + " * " + i + " = " + result);
System.out.println("The result is : " + userInput + " * " + i + " = " + result);
}
}
}
在textview中,只会最后设置textview中的文本。
设置文本,当 i=0 时,然后在 for 循环增量之后将文本追加到 textview 中。
之后你就会得到想要的结果。
if(i==0)
ans.setText("The result is : " + userInput + " * " + i + " = " + result);
else
ans.append("\nThe result is : " + userInput + " * " + i + " = " + result);
您可以使用 String
变量通过 concat
函数显示所有结果,在执行循环后只需将此 string
设置为您的 textView
:
String answerString = "";
@Override
public void onClick(View v) {
if (v.getId() == R.id.submit){
for (int i = 0; i < 10; i++)
{
int userInput, result;
userInput = Integer.parseInt(user.getText().toString());
result = userInput * i;
answerString = answerString.concat("The result is : " + userInput + " * " + i + " = " + result);
answerString = answerString.concat("\n");
}
ans.setText(answerString);
}
}
您也可以在循环前只使用一次 "The result is" 并显示结果,如:
String answerString = "";
@Override
public void onClick(View v) {
if (v.getId() == R.id.submit){
answerString = "The result is : \n";
for (int i = 0; i < 10; i++)
{
int userInput, result;
userInput = Integer.parseInt(user.getText().toString());
result = userInput * i;
answerString = answerString.concat(userInput + " * " + i + " = " + result);
answerString = answerString.concat("\n");
}
ans.setText(answerString);
}
}
你的输出将是这样的:
The result is :
2 * 0 = 0
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
替换
ans.setText("The result is : " + userInput + " * " + i + " = " + result);
和
ans.append("The result is : " + userInput + " * " + i + " = " + result + "\n");
你的问题就解决了
您可以使用 append
而不是 setText
示例:
ans.append("The result is : " + userInput + " * " + i + " = " + result + "\n");
或
您可以使用 String 变量使用 +=
连接字符串值,最后在循环 setText
之后到 TextView
示例:
String strMultiplication = ""; // declare string before for loop
for (int i = 0; i < 10; i++) {
int userInput, result;
userInput = Integer.parseInt(user.getText().toString());
result = userInput * i;
strMultiplication += "The result is : " + userInput + " * " + i + " = " + result +"\n"); //String concatenation
System.out.println("The result is : " + userInput + " * " + i + " = " + result);
}
ans.setText(strMultiplication); // setText To the textView outside the loop
package com.example.count;
import java.util.Scanner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button b1;
TextView tv;
int input=2;
int result;
String finalresult="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.txt);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String res= rint();
tv.setText(res);
}
});
}
public String rint(){
for (int i = 0; i < 10; i++) {
result = input * i;
finalresult=finalresult+"\n"+"The result is : " +input + " * " + i + " = " + result;
}
return finalresult;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="14dp"
android:inputType="textMultiLine"
android:text="@string/hello_world" />
</RelativeLayout>