質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

3回答

329閲覧

クリアボタン押すといったん消えるがタッチすると再度表示されてしまう

退会済みユーザー

退会済みユーザー

総合スコア0

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2018/10/04 05:40

編集2018/12/12 01:25
コード //Main.Activity.javaのコード import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.GestureDetector; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.PrintWriter; public class MainActivity extends AppCompatActivity { private StringBuffer info = new StringBuffer("Test onTouchEvent\n\n"); //テキストビュー private TextView textView1; private TextView textView2; private TextView textView3; private TextView textView4; private ImageView imageView1; private ImageView imageView2; private ImageView imageView3; private ImageView imageView4; private ImageView imageView5; private ImageView imageView6; private ImageView imageView7; private ImageView imageView8; private ImageView imageView9; private ImageView imageView10; private int mcount; // X軸最低スワイプ距離 private static final int SWIPE_MIN_DISTANCE = 300; // X軸最低スワイプスピード private static final int SWIPE_THRESHOLD_VELOCITY = 200; // Y軸の移動距離 これ以上なら横移動を判定しない private static final int SWIPE_MAX_OFF_PATH = 180; // タッチイベントを処理するためのインタフェース private GestureDetector mGestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = findViewById(R.id.textView1); textView2 = findViewById(R.id.textView2); textView3 = findViewById(R.id.textView3); imageView1 = findViewById(R.id.imageView1); imageView2 = findViewById(R.id.imageView2); imageView3 = findViewById(R.id.imageView3); imageView4 = findViewById(R.id.imageView4); imageView5 = findViewById(R.id.imageView5); imageView6 = findViewById(R.id.imageView6); imageView7 = findViewById(R.id.imageView7); imageView8 = findViewById(R.id.imageView8); imageView9 = findViewById(R.id.imageView9); imageView10 = findViewById(R.id.imageView10);     //クリアボタンのイベント Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { StringBuffer info = new StringBuffer("Test onTouchEvent\n\n"); textView1.setText(info); } }); } //タッチイベント @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: info.append("ACTION_DOWN\n"); info.append("Pressure"); info.append(event.getPressure()); info.append("\n"); info.append("x1:"); info.append(event.getX()); info.append("\n"); info.append("y1:"); info.append(event.getY()); info.append("\n\n"); break; case MotionEvent.ACTION_UP: info.append("ACTION_UP\n"); info.append("x2:"); info.append(event.getX()); info.append("\n"); info.append("y2:"); info.append(event.getY()); info.append("\n"); long eventDuration2 = event.getEventTime() - event.getDownTime(); info.append("duration: "); info.append(eventDuration2); info.append(" m/sec\n\n"); break; case MotionEvent.ACTION_MOVE: info.append("ACTION_MOVE\n"); break; case MotionEvent.ACTION_CANCEL: info.append("ACTION_CANCEL\n"); break; } textView1.setText(info); return false; } }
//activity_main(レイアウトのコード) <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:id="@+id/button" android:text="@string/button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="150dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="300dp"/> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="400dp"/> <ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginStart="160dp" android:layout_marginTop="130dp" app:srcCompat="@android:drawable/alert_light_frame" android:contentDescription="@string/todo"/> <ImageView android:id="@+id/imageView2" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginStart="160dp" android:layout_marginTop="650dp" app:srcCompat="@android:drawable/alert_light_frame" android:contentDescription="@string/todo" /> <ImageView android:id="@+id/imageView3" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginStart="330dp" android:layout_marginTop="130dp" app:srcCompat="@android:drawable/alert_light_frame" android:contentDescription="@string/todo"/> <ImageView android:id="@+id/imageView4" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginStart="330dp" android:layout_marginTop="650dp" app:srcCompat="@android:drawable/alert_light_frame" android:contentDescription="@string/todo"/> <ImageView android:id="@+id/imageView5" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginStart="450dp" android:layout_marginTop="230dp" app:srcCompat="@android:drawable/alert_light_frame" android:contentDescription="@string/todo"/> <ImageView android:id="@+id/imageView6" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginStart="40dp" android:layout_marginTop="230dp" app:srcCompat="@android:drawable/alert_light_frame" android:contentDescription="@string/todo"/> <ImageView android:id="@+id/imageView7" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginStart="40dp" android:layout_marginTop="550dp" app:srcCompat="@android:drawable/alert_light_frame" android:contentDescription="@string/todo"/> <ImageView android:id="@+id/imageView8" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginStart="450dp" android:layout_marginTop="550dp" app:srcCompat="@android:drawable/alert_light_frame" android:contentDescription="@string/todo"/> <ImageView android:id="@+id/imageView9" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginStart="10dp" android:layout_marginTop="385dp" app:srcCompat="@android:drawable/alert_light_frame" android:contentDescription="@string/todo"/> <ImageView android:id="@+id/imageView10" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginStart="490dp" android:layout_marginTop="385dp" app:srcCompat="@android:drawable/alert_light_frame" android:contentDescription="@string/todo"/> </FrameLayout>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2018/10/04 06:34

コードは、<code>ボタン、’’’の枠の中に貼り付けてください
m.ts10806

2018/10/04 06:59

y_waiwaiさん ``` ですよ(バッククォート3つ)
m.ts10806

2018/10/04 07:46

Javaだけの要件ではなさそうなのでAndroidなど詳細なタグも追加しておいてください。
y_waiwai

2018/10/04 07:52

クリアボタンのイベントってどれだろう。。
退会済みユーザー

退会済みユーザー

2018/10/05 06:56 編集

なんでソースコードとxmlをひとまとめに囲むんだろう・・・onTouchEvent内でtextView1.setText(info);してるからではないんですか?
退会済みユーザー

退会済みユーザー

2018/10/06 03:53

OnTouchEvent内にtextView1.setText(info)を書かないと判定してくれないので入れました。ソースコードとXMLはひとまとめにしない方がいいんですか?
m.ts10806

2018/10/06 03:57

1ファイルに全て書いてある訳じゃないですよね。
退会済みユーザー

退会済みユーザー

2018/10/06 04:34

1ファイルに書いているわけではないです。 2つに分けられると知らなかったのでまとめて投稿してしまいました。 今、Mainのコードとレイアウトのコード、二つに分けたので確認お願いします
m.ts10806

2018/12/05 11:03

解決した質問を編集で潰すとは何がしたいのでしょうか。
m.ts10806

2018/12/05 11:05

退会、ねえ・・・。編集履歴から確認できるとはいえそういう逃げ方は勘弁してもらいたいですね。回答してくれた皆さんに失礼です。
guest

回答3

0

ベストアンサー

java

1 @Override 2 public void onClick(View v) { 3 StringBuffer info = new StringBuffer("Test onTouchEvent\n\n"); 4 textView1.setText(info); 5 6 } 7 }); 8

このinfoがローカル変数であることが問題です。フィールド変数のinfoは前の状態のまま残っているため、タッチイベントでフィールド変数のinfoが再度TextViewにセットされてしまうので、この問題が起こっています。
フィールド変数のinfoから文字をすべてdeleteするか、OnClickListenerをこのActivityにimplementして操作するという方法を取ることになると思います。

投稿2018/10/06 05:13

編集2018/10/06 05:35
swordone

総合スコア20649

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/10/06 05:33

内容とは関係ないのですが、最後の「NARUTO思います」が気になりまして・・・
swordone

2018/10/06 05:35

ありがとうございます。
退会済みユーザー

退会済みユーザー

2018/10/15 05:39

回答ありがとうございました。無事にクリアボタンが機能しました。本当にありがとうございました。
guest

0

//タッチイベント @Override public boolean onTouchEvent(MotionEvent event) {

Activity#onTouchEventは、画面をタップしたときどのビューもタッチイベントを取らなければ実行されますから、この関数の一連の処理が原因では。

画面をタッチすると再度消したものが表示されてしまいます。

とのことですから、おそらく意識して書いたのではないですよね。

投稿2018/10/06 05:31

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

テストしてないしアンドロイドあんま詳しくないけど、変数でタッチされたか判定してタッチされてたら実行しない、でいいんじゃないかな?まちがってたらごめんなさい。

投稿2018/10/06 03:08

yukkuri

総合スコア624

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2018/10/06 03:59

ちょっと回答としては無責任すぎる気がします。 初心者アイコンつけてらっしゃるのである程度は考慮してあげてください
yukkuri

2018/10/06 04:00

すいません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問