gpt4 book ai didi

安卓SDK : Show one line at a time and proceed with onClick

转载 作者:行者123 更新时间:2023-11-29 16:08:55 24 4
gpt4 key购买 nike

我是 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>

主要 Activity .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 中,按钮在单击时立即加载所有文本,不支持“重新单击”它。

有什么想法吗?

最好的问候,

更新:我做了一个计数器来说明应该在屏幕上打印哪个词。我还有另一个问题:我如何着手制作 tre 单词(或任意数量的单词)来显示而不是只显示一个?我试图摆弄索引范围,但我似乎无法掌握它。这就是我的 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]);
}
});

}}

最佳答案

您的代码在每次迭代时都为 TextView 分配值,并在每次迭代后添加一个单词。这就是加载所有文本的原因。

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

如果您想在每次点击时显示一个单词,请制作一个计数器作为单词数组的索引,并在每次点击后递增。并将“当前索引词”分配给文本字段。

关于安卓SDK : Show one line at a time and proceed with onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15198521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com