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

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

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

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

Q&A

解決済

1回答

1199閲覧

異なるレイアウトにある、テキストビューを、書き換えるには? (How to change text in textView in the different layouts)

akirasada1972

総合スコア41

Android

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

0グッド

0クリップ

投稿2018/08/27 14:44

編集2018/09/04 21:46

異なるレイアウトにある、テキストビューを、書き換えるには、どうしたらよいのでしょうか?
レイアウトを示す何かを、書き込まなくては、ならないのかと思うのですが、どう書いたらよいのでしょうか?
(I'd like to ask how to change text in textView in the different layouts.)

java

1package vc.ddns.luna.sexydesign.screeentoucher; 2 3import android.app.Activity; 4import android.support.v7.app.AppCompatActivity; 5import android.os.Bundle; 6import android.view.View; 7import android.view.View.OnClickListener; 8import android.widget.Button; 9 10public class MainActivity extends Activity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 16 setContentView(R.layout.activity_main); 17 18 findViewById(R.id.view).setVisibility(View.INVISIBLE); 19 20 // ボタンのオブジェクトを取得 21 Button btn1 = (Button) findViewById(R.id.button1); 22 Button btn2 = (Button) findViewById(R.id.button2); 23 24 // クリックイベントを受け取れるようにする 25 btn1.setOnClickListener(new OnClickListener() { 26 // このメソッドがクリック毎に呼び出される 27 public void onClick(View v) { 28 // ここにクリックされたときの処理を記述 29 findViewById(R.id.view).setVisibility(View.VISIBLE); 30 } 31 }); 32 // クリックイベントを受け取れるようにする 33 btn2.setOnClickListener(new OnClickListener() { 34 // このメソッドがクリック毎に呼び出される 35 public void onClick(View v) { 36 // ここにクリックされたときの処理を記述 37 findViewById(R.id.view).setVisibility(View.INVISIBLE); 38 } 39 }); 40 } 41}

java

1package vc.ddns.luna.sexydesign.screeentoucher; 2 3import android.content.Context; 4import android.graphics.Canvas; 5import android.graphics.Color; 6import android.graphics.Paint; 7import android.util.AttributeSet; 8import android.view.MotionEvent; 9import android.view.View; 10import android.widget.TextView; 11 12public class Circles extends View implements View.OnTouchListener { 13 14 Paint paint; 15 16 static int r = 100; 17 static int score = 0; 18 static int x; 19 static int y; 20 21 final static int bc = Color.BLACK; 22 final static int dc = Color.MAGENTA; 23 24 public Circles(Context context, AttributeSet attrs) { 25 super(context, attrs); 26 paint = new Paint(); 27 } 28 @Override 29 protected void onDraw(Canvas canvas) { 30 super.onDraw(canvas); 31 32 final int width = canvas.getWidth(); 33 final int height = canvas.getHeight(); 34 35 x = (int) (Math.random() * width); 36 y = (int) (Math.random() * height); 37 if ((x < width - 2 * r) && (y < height - 2 * r)) { 38 canvas.drawColor(bc); 39 paint.setColor(dc); 40 canvas.drawCircle(x+r, y+r, r, paint); 41 findViewById(R.id.view).setOnTouchListener(this); 42 } else { 43 invalidate(); 44 } 45 } 46 47 @Override 48 public boolean onTouch(View v, MotionEvent e) { 49 double mouseX = e.getX(); 50 double mouseY = e.getY(); 51 if (mouseX > x && mouseX < x + 2 * r) { 52 if (mouseY > y && mouseY < y + 2 * r) { 53 invalidate(); 54 55 TextView textView = findViewById(R.id.textView); 56 textView.setText(String.valueOf(score)); 57 score+=100; 58 } 59 } 60 return false; 61 } 62

xml

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:orientation="vertical"> 13 14 <LinearLayout 15 android:layout_width="match_parent" 16 android:layout_height="50dp" 17 android:orientation="horizontal" 18 android:id="@+id/layout" > 19 20 <Button 21 android:id="@+id/button1" 22 android:layout_width="match_parent" 23 android:layout_height="match_parent" 24 android:layout_weight="1" 25 android:background="@android:color/holo_red_dark" 26 android:text="Start" 27 android:textColor="@android:color/white" 28 android:textSize="30sp" 29 tools:layout_editor_absoluteX="0dp" 30 tools:layout_editor_absoluteY="34dp" /> 31 32 <TextView 33 android:id="@+id/textView" 34 android:layout_width="match_parent" 35 android:layout_height="match_parent" 36 android:layout_weight="1" 37 android:background="@android:color/holo_green_light" 38 android:gravity="right|center_vertical" 39 android:paddingLeft="10dp" 40 android:paddingRight="10dp" 41 android:textColor="@android:color/white" 42 android:textSize="30sp" 43 tools:layout_editor_absoluteX="112dp" 44 tools:layout_editor_absoluteY="54dp" /> 45 46 <Button 47 android:id="@+id/button2" 48 android:layout_width="match_parent" 49 android:layout_height="match_parent" 50 android:layout_weight="1" 51 android:background="@android:color/holo_blue_dark" 52 android:text="End" 53 android:textColor="@android:color/white" 54 android:textSize="30sp" 55 tools:layout_editor_absoluteX="224dp" 56 tools:layout_editor_absoluteY="54dp" /> 57 </LinearLayout> 58 59 <vc.ddns.luna.sexydesign.screeentoucher.Circles 60 android:id="@+id/view" 61 android:layout_width="match_parent" 62 android:layout_height="match_parent" 63 android:background="@android:color/darker_gray" /> 64 </LinearLayout> 65 66</android.support.constraint.ConstraintLayout>

問題の部分です。 (The place where problems exist.)
問題の部分

下記のようにも試したのですが、だめでした。
試したこと2
試したこと

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

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

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

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

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

guest

回答1

0

自己解決

findViewByIdを、MainActivityのみに、限定したところ、うまくいきました。

java

1package vc.ddns.luna.sexydesign.screeentoucher; 2 3import android.app.Activity; 4import android.os.Bundle; 5import android.view.MotionEvent; 6import android.view.View; 7import android.view.View.OnClickListener; 8import android.widget.Button; 9import android.widget.TextView; 10 11public class MainActivity extends Activity { 12 13 private Circles circles; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 19 setContentView(R.layout.activity_main); 20 21 Button btn1 = (Button) findViewById(R.id.button1); 22 Button btn2 = (Button) findViewById(R.id.button2); 23 24 findViewById(R.id.view).setVisibility(View.INVISIBLE); 25 26 btn1.setOnClickListener(new OnClickListener() { 27 // このメソッドがクリック毎に呼び出される 28 public void onClick(View v) { 29 30 findViewById(R.id.view).setVisibility(View.VISIBLE); 31 32 findViewById(R.id.view).setOnTouchListener(new View.OnTouchListener() { 33 int count = 0; 34 @Override 35 public boolean onTouch(View v, MotionEvent e) { 36 double mouseX = e.getX(); 37 double mouseY = e.getY(); 38 circles = findViewById(R.id.view); 39 int x = circles.x; 40 int y = circles.y; 41 int r = circles.r; 42 43 if (mouseX > x && mouseX < x + 2 * r) { 44 if (mouseY > y && mouseY < y + 2 * r) { 45 circles.invalidate(); 46 count += 100; 47 TextView tv = (TextView) findViewById(R.id.textView); 48 tv.setText(String.valueOf(count)); 49 50 } 51 } 52 return false; 53 } 54 }); 55 } 56 }); 57 58 btn2.setOnClickListener(new OnClickListener() { 59 // このメソッドがクリック毎に呼び出される 60 public void onClick(View v) { 61 findViewById(R.id.view).setVisibility(View.INVISIBLE); 62 } 63 }); 64 } 65}

java

1package vc.ddns.luna.sexydesign.screeentoucher; 2 3import android.content.Context; 4import android.graphics.Canvas; 5import android.graphics.Color; 6import android.graphics.Paint; 7import android.util.AttributeSet; 8import android.view.View; 9 10public class Circles extends View { 11 12 private Paint paint; 13 14 public Circles (Context context, AttributeSet attrs) { 15 super(context, attrs); 16 paint =new Paint(); 17 } 18 19final int r = 100; 20int x; 21int y; 22 23final int bc = Color.BLACK; 24final int dc = Color.MAGENTA; 25 26protected void onDraw(Canvas canvas) { 27 28 final int width = canvas.getWidth(); 29 final int height = canvas.getHeight(); 30 31 x = (int) (Math.random() * width); 32 y = (int) (Math.random() * height); 33 34 if ((x < width - 2 * r) && (y < height - 2 * r)) { 35 canvas.drawColor(bc); 36 paint.setColor(dc); 37 canvas.drawCircle(x + r, y + r, r, paint); 38// ll1 = (LinearLayout)findViewById(R.id.layout1); 39// ll2 = (LinearLayout)ll1.findViewById(R.id.layout2); 40// findViewById(R.id.view).setOnTouchListener(this); 41 } else { 42 invalidate(); 43 } 44} 45 46}

xml

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:orientation="vertical" 13 android:id="@+id/layout1"> 14 15 <LinearLayout 16 android:layout_width="match_parent" 17 android:layout_height="50dp" 18 android:orientation="horizontal" 19 android:id="@+id/layout2" > 20 21 <Button 22 android:id="@+id/button1" 23 android:layout_width="match_parent" 24 android:layout_height="match_parent" 25 android:layout_weight="1" 26 android:background="@android:color/holo_red_dark" 27 android:text="Start" 28 android:textColor="@android:color/white" 29 android:textSize="30sp" 30 tools:layout_editor_absoluteX="0dp" 31 tools:layout_editor_absoluteY="34dp" /> 32 33 <TextView 34 android:id="@+id/textView" 35 android:layout_width="match_parent" 36 android:layout_height="match_parent" 37 android:layout_weight="1" 38 android:background="@android:color/holo_green_light" 39 android:gravity="right|center_vertical" 40 android:paddingLeft="10dp" 41 android:paddingRight="10dp" 42 android:text="0" 43 android:textColor="@android:color/white" 44 android:textSize="30sp" 45 tools:layout_editor_absoluteX="112dp" 46 tools:layout_editor_absoluteY="54dp" /> 47 48 <Button 49 android:id="@+id/button2" 50 android:layout_width="match_parent" 51 android:layout_height="match_parent" 52 android:layout_weight="1" 53 android:background="@android:color/holo_blue_dark" 54 android:text="End" 55 android:textColor="@android:color/white" 56 android:textSize="30sp" 57 tools:layout_editor_absoluteX="224dp" 58 tools:layout_editor_absoluteY="54dp" /> 59 </LinearLayout> 60 61 <vc.ddns.luna.sexydesign.screeentoucher.Circles 62 android:id="@+id/view" 63 android:layout_width="match_parent" 64 android:layout_height="match_parent" /> 65 </LinearLayout> 66 67</android.support.constraint.ConstraintLayout>

投稿2018/09/08 19:40

akirasada1972

総合スコア41

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問