###前提・実現したいこと
Android Studioで制作しているアプリケーションとして、EditText内の文字を一行ごとに配列に入れ、表示ボタンを押すとTextViewに順番に表示し直すといったものを作ろうと考えました。
そこで、こちらのサイトを参考にして、実現させようと考えました。
このサイトの場合、JTextArea内の文字を一行ごとに配列に入れ、別のJTextAreaに表示しなおすというもので、
こんにちは。(改行) おはようございます。お元気ですか。(改行) はい、おかげさまで元気です。
と入力すると、
1:こんにちは。 2:おはようございます。お元気ですか。 3:はい、おかげさまで元気です。
と表示されます。
###発生している問題・エラーメッセージ
しかし、自分が作ったアプリでは、サイトのような表示はなされませんでした。
1:こんにちは。 おはようございます。お元気ですか。 はい、おかげさまで元気です。¥n
そこで、私のアプリのコードにおかしなところがあれば教えていただきたいと考えました。もしかしたら、サイトで書かれているやり方は、JTextAreaでしか実現不可能なものなのでしょうか。もしそうなのであれば、よろしければ、Androidのアプリケーション上で、EditText内の文字を一行ごとに配列に入れて表示させるにはどのようにしたらよいか、教えていただけないでしょうか。よろしくお願いします。
###該当のソースコード
「MainActivity.java」
package com.example.(諸事情により隠させていただきます); import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.text.SpannableStringBuilder; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); } public void onClickButton1(View view) { EditText edit = (EditText)findViewById(R.id.editText); SpannableStringBuilder sp = (SpannableStringBuilder)edit.getText(); String str = sp.toString(); String[] strs = str.split("¥n"); StringBuilder sb = new StringBuilder(); for (int i = 0 ; i < strs.length ; i++){ sb.append((i + 1) + ":" + strs[i] + "¥n"); } textView.setText(new String(sb)); } }
「activity_main.xml」
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="com.example.(諸事情により隠させていただきます)" xmlns:android="http://schemas.android.com/apk/res/android"> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:ems="10" android:inputType="text|textMultiLine" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:onClick="onClickButton1" android:text="表示" android:layout_below="@+id/editText" app:layout_constraintRight_toRightOf="parent" tools:layout_editor_absoluteY="59dp" /> <ScrollView android:layout_width="368dp" android:layout_height="388dp" android:layout_below="@+id/button" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="115dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </ScrollView> </RelativeLayout>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/29 17:55