我是 Java 和 Android SDK 的新手,但我愿意学习。我想要做的是构建一个应用程序,它接受一个句子或一个通用字符串并且只显示一行文本,这已经编码。但我想做的是通过单击一个名为“继续”之类的按钮进入“下一个”行。所以简而言之,它只是让您一次阅读少量文本,然后单击按钮继续。
这是我已经走了多远:
activity_main.xml
<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=".DisplayMessageActivity"
android:orientation="horizontal" >
<TextView
android:id="@+id/text_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:maxLines="1" />
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/next"
android:onClick="nextLine"
android:textSize="15sp" />
</RelativeLayout>
MainActivity.java:
package com.xxx.xxx.xxx;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s = "Iphone swag fanny pack, try-hard master cleanse pop-up elit deserunt vinyl. Odio mustache hoodie YOLO fap. Vegan godard labore tempor. Farm-to-table aute craft beer, YOLO bicycle rights artisan semiotics. Cosby sweater readymade eiusmod consectetur fap stumptown. Cliche keytar accusamus blue bottle, wayfarers locavore selfies elit aliqua chillwave lo-fi helvetica enim. Irony tofu blog, fap pickled pariatur odio wolf cliche vice sint pitchfork eiusmod cosby sweater polaroid.";
final String[] words = s.split("\\s+");
Button b = (Button) findViewById(R.id.next_button);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.text_box);
StringBuilder builder = new StringBuilder();
for (String s : words){
builder.append(s+" ");
tx.setText(builder.toString());
}
}});
}}
到目前为止,没有文本加载到 textView 中,并且按钮在单击时一次加载所有文本,不支持“重新单击”它。
有任何想法吗?
此致,
更新: 我做了一个计数器来说明应该在屏幕上打印哪个单词。我还有一个问题:我如何才能显示三个单词(或任意数量的单词)而不是一个?我试图摆弄 indexranges,但我似乎无法掌握它。这就是我的 MainActivity.java 现在的样子:
package com.xxx.xxx.xxx;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
String s = "Iphone swag fanny pack, try-hard master cleanse pop-up elit deserunt vinyl. Odio mustache hoodie YOLO fap. Vegan godard labore tempor. Farm-to-table aute craft beer, YOLO bicycle rights artisan semiotics. Cosby sweater readymade eiusmod consectetur fap stumptown. Cliche keytar accusamus blue bottle, wayfarers locavore selfies elit aliqua chillwave lo-fi helvetica enim. Irony tofu blog, fap pickled pariatur odio wolf cliche vice sint pitchfork eiusmod cosby sweater polaroid.";
final String[] words = s.split("\\s+");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tx = (TextView) findViewById(R.id.text_box);
counter = 0;
tx.setText(words[counter]);
Button b = (Button) findViewById(R.id.next_button);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
counter++;
tx.setText(words[counter]);
}
});
}}