使用1 textview显示文本列表

时间:2015-02-04 03:47:21

标签: android textview

我正在尝试在同一文本视图中显示结果列表。

下面是我的代码......这段代码有效,但只显示for循环的最后一个值。我需要代码来打印以下内容。

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);


        }

    }
}

5 个答案:

答案 0 :(得分:2)

您可以使用String变量通过concat函数显示所有结果,并在执行循环后将此string设置为textView,如:< / p>

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);

    }
}

您也可以在循环前只使用“结果为”一次,并显示如下结果:

 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

答案 1 :(得分:1)

在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);

答案 2 :(得分:1)

替换

ans.setText("The result is : " + userInput + " * " + i + " = " + result);

ans.append("The result is : " + userInput + " * " + i + " = " + result + "\n");

你的问题解决了

答案 3 :(得分:1)

您可以使用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

答案 4 :(得分:1)

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>