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

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

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

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

Q&A

0回答

1048閲覧

[Android Studio] TouchEventを実装できない

Shibou

総合スコア15

Java

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

0グッド

0クリップ

投稿2020/04/23 07:00

カレンダーアプリを制作をしており、
日付を表示しているTextViewをタッチして背景色を変えようとしているのですが、
View.OnTouchListenerを実装しても反応しません。
どこが間違っているのかわからないので、ご教授いただければ幸いです。

java

1package com.example.calendar; 2 3import androidx.appcompat.app.AppCompatActivity; 4 5import android.graphics.Color; 6import android.graphics.drawable.Drawable; 7import android.os.Bundle; 8import android.view.MotionEvent; 9import android.view.View; 10import android.widget.TableRow; 11import android.widget.TextView; 12 13import java.util.List; 14 15public class MainActivity extends AppCompatActivity implements View.OnTouchListener { 16 //CalendarMaker 17 CalendarMaker calendarMaker; 18 //日付のID 19 int[][]dateIdies ={ 20 {R.id.one_one,R.id.one_two,R.id.one_three,R.id.one_four,R.id.one_five,R.id.one_six,R.id.one_seven}, 21 {R.id.two_one,R.id.two_two,R.id.two_three,R.id.two_four,R.id.two_five,R.id.two_six,R.id.two_seven}, 22 {R.id.three_one,R.id.three_two,R.id.three_three,R.id.three_four,R.id.three_five,R.id.three_six,R.id.three_seven}, 23 {R.id.four_one,R.id.four_two,R.id.four_three,R.id.four_four,R.id.four_five,R.id.four_six,R.id.four_seven}, 24 {R.id.five_one,R.id.five_two,R.id.five_three,R.id.five_four,R.id.five_five,R.id.five_six,R.id.five_seven}, 25 {R.id.six_one,R.id.six_two,R.id.six_three,R.id.six_four,R.id.six_five,R.id.six_six,R.id.six_seven}, 26 }; 27 //日付情報を格納するList 28 List<String> dateList = null; 29 30 //今日の日付の背景情報を保存 31 int todayID = 0; //背景を変えたTextViewのID 32 Drawable todayDrawable = null; //元の背景 33 34 //選択された日付の背景情報を保存 35 int chooseID = 0; 36 Drawable chooseDrawable = null; 37 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.activity_main); 42 //CalendarMakerの生成 43 calendarMaker = new CalendarMaker(); 44 //カレンダーを生成 45 this.createCalendar(); 46 } 47 //次月へ移動するボタン 48 public void onNext(View v){ 49 //月を進める 50 calendarMaker.nextMonth(); 51 //カレンダーを生成 52 createCalendar(); 53 } 54 //先月へ移動するボタン 55 public void onPrevious(View v){ 56 //月を戻す 57 calendarMaker.previousMonth(); 58 //カレンダーを生成 59 createCalendar(); 60 } 61 //カレンダーを組み立てるメソッド 62 private void createCalendar(){ 63 dateList = calendarMaker.getList(); 64 //背景を変えていたTextViewがある場合 65 if(this.todayID != 0 && this.todayDrawable != null){ 66 TextView textView = ((TextView)findViewById(this.todayID)); 67 textView.setBackground(todayDrawable); 68 } 69 //List要素取り出しようカウント 70 int count = 0; 71 //表示する月に今日の日付があるか 72 boolean haveToday = calendarMaker.haveToday(); 73 for(int i = 0;i < 6;i++){ 74 for(int j = 0; j < 7;j++){ 75 //日付のTextViewを生成 76 TextView textView = ((TextView)findViewById(dateIdies[i][j])); 77 textView.setText(dateList.get(count)); //日付情報を格納する 78 //今日の日付の背景を変える 79 if(haveToday && textView.getText(). 80 toString().equals(String.valueOf(calendarMaker.getToday()))){ 81 //背景を変えるTextViewのidを保存 82 this.todayID = textView.getId(); 83 //元の色を保存 84 this.todayDrawable = textView.getBackground(); 85 //背景の色を変える 86 textView.setBackgroundColor(Color.RED); 87 //タッチイベントの設定 88 textView.setOnTouchListener(this); 89 } 90 //6週目の日曜日を設定した場合 91 if(i == 5 && j == 0 ){ 92 TableRow tableRow = ((TableRow)findViewById(R.id.lastWeek)); 93 //6週目の日曜日が空欄ならば 94 if(textView.getText().toString().equals("")){ 95 //6週目を不可視にする 96 tableRow.setVisibility(View.GONE); 97 }else{ 98 //6週目を可視化する 99 tableRow.setVisibility(View.VISIBLE); 100 } 101 } 102 count++; 103 } 104 } 105 //表示する年月をセット 106 ((TextView)findViewById(R.id.display_year_month)).setText(calendarMaker.toString()); 107 } 108 109 @Override 110 public boolean onTouch(View v, MotionEvent event) { 111 switch(event.getAction()){ 112 case MotionEvent.ACTION_DOWN: 113 //選択したviewのidと背景を保存する 114 this.chooseID = v.getId(); 115 this.chooseDrawable = v.getBackground(); 116 //選択したviewの背景を変える 117 v.setBackgroundColor(Color.BLACK); 118 return true; 119 } 120 return false; 121 } 122} 123

java

1package com.example.calendar; 2 3import java.util.ArrayList; 4import java.util.Calendar; 5 6public class CalendarMaker { 7 private Calendar calendar = null; 8 9 //現在の日付 10 private int nowYear = 0; //年 11 private int nowMonth = 0; //月 12 private int nowDay = 0; //日 13 14 //表示するカレンダーの年月 15 private int displayYear = 0; //年 16 private int displayMonth = 0; //月 17 18 //表示するカレンダーの必要情報 19 private int firstWeeks = 0; //月初めの曜日 20 private int lastDay = 0; //月の最終日 21 //表示用のフォーマット 22 private final String DISPLAY_FORMAT = "%04d年%02d月"; 23 //コンストラクタ 24 public CalendarMaker(){ 25 //現在日時の取得 26 calendar = Calendar.getInstance(); 27 //現在日時のセット 28 this.nowYear = calendar.get(Calendar.YEAR); 29 this.nowMonth = calendar.get(Calendar.MONTH); 30 this.nowDay = calendar.get(Calendar.DATE); 31 //表示年月をセット 32 this.displayYear = this.nowYear; //表示年 33 this.displayMonth = this.nowMonth; //表示月 34 //カレンダー生成の必要情報を取得 35 this.getNeccessary(); 36 } 37 //日付情報を格納したListを生成して返すメソッド 38 public ArrayList<String> getList(){ 39 ArrayList<String>list = new ArrayList<>(); 40 int dateCount = 1; //格納する日 41 //1週目を作成 42 for(int i = 1; i <= 7; i++){ 43 if(i < this.firstWeeks){ 44 list.add(""); //空白を入れる 45 }else{ 46 list.add(String.valueOf(dateCount)); //日付を入れる 47 dateCount++; //日付のカウントを進める 48 } 49 } 50 //2週目以降を埋める 51 for(int j = 8; j <= 42; j++){ 52 //最終日まで日付を格納していない場合 53 if(dateCount <= lastDay){ 54 list.add(String.valueOf(dateCount)); 55 dateCount++; 56 }else{ 57 list.add(""); 58 } 59 } 60 //42の日付情報を格納したListを返す 61 return list; 62 } 63 //表示する年月を生成して渡す 64 public String toString(){ 65 return String.format(DISPLAY_FORMAT,displayYear,displayMonth+1); 66 } 67 68 //月をひとつ進める 69 public void nextMonth(){ 70 this.displayMonth++; 71 //月を進めて年をまたいだ場合 72 if(displayMonth > 11){ 73 this.displayMonth = 0; //月を1月にセット 74 this.displayYear++; //年を一つ進める 75 } 76 //必要情報の更新 77 this.getNeccessary(); 78 } 79 //月を一つ戻す 80 public void previousMonth(){ 81 this.displayMonth--; 82 //月を戻して年をまたいだ場合 83 if(displayMonth < 0){ 84 this.displayMonth = 11; 85 this.displayYear--; 86 } 87 //必要情報の更新 88 this.getNeccessary(); 89 } 90 //表示されている月に本日の日付が含まれるか否か 91 public boolean haveToday(){ 92 boolean doYouHave = false; 93 if(this.nowYear == this.displayYear && this.nowMonth == this.displayMonth){ 94 doYouHave = true; 95 } 96 return doYouHave; 97 } 98 //今日の日付を返す 99 public int getToday(){ 100 return this.nowDay; 101 } 102 //月初めの曜日を取得 103 private void getFirstWeeks(){ 104 calendar.set(displayYear,displayMonth,1); //表示年月の初日にセット 105 this.firstWeeks = calendar.get(Calendar.DAY_OF_WEEK); 106 } 107 //月末の日にちを取得 108 private void getLastDay(){ 109 calendar.add(Calendar.MONTH,1); 110 calendar.add(Calendar.DATE,-1); 111 this.lastDay = calendar.get(Calendar.DATE); 112 } 113 //必要情報の取得メソッド 114 private void getNeccessary(){ 115 this.getFirstWeeks(); 116 this.getLastDay(); 117 }

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

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

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

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

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

hoshi-takanori

2020/04/23 08:01

今日の日付の場合のみ setOnTouchListener してるようですが、今日の日付をタッチしても反応がないのでしょうか? また、OnTouch よりも OnClick の方が適切な気がします。
keicha_hrs

2020/04/23 08:04

setOnTouchListener()がfor文の中に存在するようですが、これによるイベントが発生しないという質問なのでしょうか?Android Studioのデバッグ機能などを用いて、このsetOnTouchListener()の行には間違いなく到達していることは確認されていますか?
Shibou

2020/04/23 08:11

間違って今日の日付のみに設定していたようです...。 OnClickにして修正します。ありがとうございましたm(__)m
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問