1

私はJavaとAndroidSDKを初めて使用しますが、学習したいと思っています。私がやりたいのは、文または一般的な文字列を受け取り、1行のテキストのみを表示するアプリを作成することです。これはすでにコーディングされています。しかし、私がやりたいのは、「続行」などのボタンをクリックして「次の」行に進むことです。つまり、一度に少量のテキストを読んで、ボタンをクリックして続行することを提供しているだけです。

これは私がどこまで持っているかです:

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に読み込まれず、ボタンをクリックするとすべてのテキストが一度に読み込まれ、「再クリック」はサポートされません。

何か案は?

よろしくお願いします、

更新: 画面に印刷する単語を説明するためのカウンターを作成しました。別の質問があります。1つだけではなく、3つの単語(または任意の数の単語)を表示するにはどうすればよいですか?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]);
       }
    });

}}
4

2 に答える 2

2

まず、TextView tx = (TextView) findViewById(R.id.text_box);onCreateメソッドに移動します。ボタンをクリックするたびにテキスト ビューを取得する必要はありません。したがって、テキスト ビューはクラスの属性として格納する必要があります。さらに、テキストが正しく分割されていることを確認してください。最後に必要なのは、ちょっとしたロジックです。テーブル内の要素数を取得しwordsます。現在の単語に設定されているカウンターも作成します。ボタンをクリックするたびに、カウンター (words[i]) が指す単語を取得し、そのカウンターをインクリメントします。カウンターが要素の最大数に達すると、カウンターを 0 にリセットできます。

于 2013-03-04T10:20:30.147 に答える
2

コードは、反復ごとに TextView 値を割り当て、各反復の後に 1 つの単語を追加します。そのため、すべてのテキストが読み込まれます。

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

クリックごとに 1 つの単語を表示する場合は、単語配列のインデックスとして機能し、クリックごとにインクリメントするカウンターを作成します。そして、「現在のインデックス ワード」をテキスト フィールドに割り当てます。

于 2013-03-04T10:10:10.487 に答える