如何循环数组内容并在文本视图中逐行显示
java代码
package com.stuff;
import android.app.Activity;
import android.os.Bundle;
import android.provider.UserDictionary;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ScrumActivity extends Activity
{
TextView textout;
EditText inputBox;
Button okButton;
ProgressBar progressBar;
Scrumer scrumbler = new Scrumer();
StringBuilder sb = new StringBuilder();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Get elements from XML to use in application
textout = (TextView) findViewById(R.id.textView1);
inputBox = (EditText) findViewById(R.id.inputBox);
okButton = (Button) findViewById(R.id.button1);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//progressBar.setVisibility(View.VISIBLE);
SpannableStringBuilder sb = new SpannableStringBuilder();
String regularText = "This text is ";
String clickableText = "clickable";
sb.append(regularText);
sb.append(clickableText);
sb.setSpan(new ClickableSpan(), sb.length()-clickableText.length(), sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//tv.setText(sb);
//setting a listener for the button to be clicked
okButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//textout.setText(inputBox.getText().toString());
scrumer.jumble(inputBox.getText().toString());
for(String s : scrumbler.getMatchArray())
sb.append(s+"\n");
textout.setText("ok");
System.out.print(sb.toString());
}
});
}
}
继承我的main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<EditText
android:id="@+id/inputBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Search" />
<TextView
android:id="@+id/textView1"
android:layout_width="184dp"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
答案 0 :(得分:3)
StringBuilder builder = new StringBuilder();
String[] arr = {"These","are","some","words"};
for (String s : arr){
builder.append(s).append(" ");
textview.setText(builder.toString());
将These are some words
放入TextView。如果您想在每个字符串后面换行,请使用builder.append(s).append("\n");