異なるレイアウトにある、テキストビューを、書き換えるには、どうしたらよいのでしょうか?
レイアウトを示す何かを、書き込まなくては、ならないのかと思うのですが、どう書いたらよいのでしょうか?
(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>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。