1
我是Android中的諾布,我希望在文本中按順序放置一個文本,字符之間已知時間,從EditText中的書寫文本開始如何從一個ArrayList中逐個字符地設置一個TextView
這就是我所做的解決方案:我寫了兩個ArrayList,它們是從EditText中收取的,第一個是EditText中的字符,第二個是整數,用於確定字符之間的時間。 然後我解析ArrayLists,時間整數次加載順序完成,但不是字符,只有當循環結束時才繪製TextView。
我的代碼 在MainActivity:
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView showCharacter;
private TextView showAppendCharacter;
private EditText incomingText;
private Button readTextEdit;
private ArrayList<CharSequence> toText = new ArrayList<CharSequence>();
private ArrayList<Integer> timePlay = new ArrayList<Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showCharacter = (TextView) findViewById(R.id.showCharacterTextView);
showAppendCharacter = (TextView) findViewById(R.id.showAppendCharacterTextView);
incomingText = (EditText) findViewById(R.id.incomingEditText);
readTextEdit = (Button) findViewById(R.id.readTextButton);
readTextEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
toText.clear();
timePlay.clear();
showAppendCharacter.setText("");
String text = incomingText.getText().toString();
for (int base = 0; base < text.length(); base++) {
if (String.valueOf(text.charAt(base)).equals("a")) {
toText.add(("a"));
timePlay.add(500);
} else if (String.valueOf(text.charAt(base)).equals("b")) {
toText.add(("b"));
timePlay.add(650);
} else if (String.valueOf(text.charAt(base)).equals("c")) {
toText.add(("c"));
timePlay.add(800);
} else {
toText.add(("_"));
timePlay.add(1000);
}
}
for (int pos = 0; pos < toText.size(); pos++) {
try {
Thread.sleep(timePlay.get(pos));
showCharacter.setText((String) toText.get(pos));
showAppendCharacter.append((String) toText.get(pos));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
}
的activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/showCharacterTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/showCharacterTextView"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/showAppendCharacterTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/showAppendCharactersTextView"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/incomingEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/incomingTextEditText"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/readTextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/readButton" />
的strings.xml中:
<?xml version="1.0" encoding="utf-8"?>
項
<string name="app_name">texto Desde ArrayList</string>
<string name="action_settings">Settings</string>
<string name="showCharacterTextView">Show last Character</string>
<string name="showAppendCharactersTextView">Show append Characters</string>
<string name="incomingTextEditText">Incoming text</string>
<string name="readButton">Read text</string>
任何建議?? 我會很感激任何想法。
太棒了!工作中!謝謝!在開始時我有一個錯誤java.lang.ClassCastException:android.widget.TextView不能轉換爲com.prueba.textodesdearraylist.CustomTextView,然後我改變了佈局xml
Rafix