简体   繁体   English

为什么我不能为我的RelativeLayout设置onTouchListener?

[英]Why cant i set the onTouchListener for my RelativeLayout?

I am trying to make a basic app in which you touch the screen and it changes the background color of the screen. 我正在尝试制作一个基本的应用程序,您可以在其中触摸屏幕,并且它会更改屏幕的背景色。 However I can't set the OnTouchListener for the RelativeLayout that is containing my app essentials. 但是,我无法为包含我的应用程序要领的RelativeLayout设置OnTouchListener。 The app throws an error upon building and then crashes, not sure whats going on here =/ If anyone could help that would be great =) 该应用程序在构建时引发错误,然后崩溃,不确定这里发生了什么= /如果有人可以帮助,那将很棒=)

Java Code: Java代码:

public class ColorCrazeActivity extends AppCompatActivity{

int pointCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout1);
    myLayout.setOnTouchListener(
            new RelativeLayout.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent m) {
                    if (m.getActionMasked() == MotionEvent.ACTION_DOWN) {
                        changeColor();
                    }

                    return true;
                }
            }
    );

    setContentView(myLayout);
}

public void changeColor(){
    int myNum1 = (int)Math.floor(Math.random() * (255));
    int myNum2 = (int)Math.floor(Math.random() * (255));
    int myNum3 = (int)Math.floor(Math.random() * (255));

    TextView myTextView = (TextView)findViewById(R.id.myTextView);

    RelativeLayout myRelativeLayout = (RelativeLayout)findViewById(R.id.myRelativeLayout1);

    myTextView.setText("RGB(" + Integer.toString(myNum1) + ", " + Integer.toString(myNum2) + ", " + Integer.toString(myNum3) + ")");
    myRelativeLayout.setBackgroundColor(Color.argb(255, myNum1, myNum2, myNum3));
    addPoints();
}

public void addPoints(){
    pointCount += 1;

    TextView myTextView1 = (TextView)findViewById(R.id.myTextView1);
    myTextView1.setText(Integer.toString(pointCount));
}

} }

XML Code: XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/myRelativeLayout"
tools:context="com.toymakersdev.colorcraze.ColorCrazeActivity"
android:focusableInTouchMode="true"
android:background="#000c65">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/myRelativeLayout1"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myTextView1"
        android:text="Taps: 0"
        android:textSize="40dp"
        android:textColor="#ffffff"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myTextView"
        android:text="Tap here"
        android:textSize="50dp"
        android:textAlignment="center"
        android:textColor="#ffffff"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

I have a RelativeLayout nested inside of another RelativeLayout because i thought the error was because it was the root view originally. 我有一个RelativeLayout嵌套在另一个RelativeLayout内,因为我认为错误是因为它最初是根视图。 Apparently not considering its still throwing the error. 显然没有考虑它仍然抛出错误。

Set first the setContentView before do something with it: 首先设置setContentView,然后再执行以下操作:

For example: 例如:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.id.myLayout); //name of the layout file
}

Your activity looks now like this: 您的活动现在看起来像这样:

public class ColorCrazeActivity extends AppCompatActivity{

int pointCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.id.myLayout); //name of the layout file

    RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout1);
   myLayout.setOnTouchListener(
        new RelativeLayout.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent m) {
                if (m.getActionMasked() == MotionEvent.ACTION_DOWN) {
                    changeColor();
                }

                return true;
            }
        }
);


   }

public void changeColor(){
int myNum1 = (int)Math.floor(Math.random() * (255));
int myNum2 = (int)Math.floor(Math.random() * (255));
int myNum3 = (int)Math.floor(Math.random() * (255));

TextView myTextView = (TextView)findViewById(R.id.myTextView);

RelativeLayout myRelativeLayout = (RelativeLayout)findViewById(R.id.myRelativeLayout1);

myTextView.setText("RGB(" + Integer.toString(myNum1) + ", " + Integer.toString(myNum2) + ", " + Integer.toString(myNum3) + ")");
myRelativeLayout.setBackgroundColor(Color.argb(255, myNum1, myNum2, myNum3));
addPoints();
   }

public void addPoints(){
    pointCount += 1;

    TextView myTextView1 = (TextView)findViewById(R.id.myTextView1);
    myTextView1.setText(Integer.toString(pointCount));
}

Set the content view before accessing the layout 在访问布局之前设置内容视图

setContentView(R.layout.activity_layout);//adjust this xml layout

RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout1);
myLayout.setOnTouchListener(
        new RelativeLayout.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent m) {
                if (m.getActionMasked() == MotionEvent.ACTION_DOWN) {
                    changeColor();
                }

                return true;
            }
        }
);

If you don't do this first, your activity has no idea where to find the view. 如果您不先执行此操作,则您的活动将不知道在哪里可以找到视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能为我的relativeLayout对象设置自定义大小 - Why I can not set a custom size for my relativeLayout object 为什么没有为我设置OnTouchListener? - Why OnTouchListener is not set for me? 为什么当我增加宽度时,我的 RelativeLayout 没有延伸到左边的集合? xml - Why my RelativeLayout does not extend to the left set as I increase the width? xml Android无法在NestedScrollView中的RelativeLayout中将高度设置为ListView - Android cant set height to ListView in RelativeLayout inNestedScrollView 为什么在片段中为relativelayout设置backgroundcolor时按钮会更改颜色? - Why button change color when i set a backgroundcolor to the relativelayout in fragment? 为什么在这里我无法将ImageBitmap设置为RelativeLayout? - Why I am unable to set ImageBitmap to RelativeLayout here? 为什么我的onTouchListener被称为邻居视图? - Why is my onTouchListener called for neighbour views? 如果更改了RelativeLayout的背景,为什么Layout的项目位置也会改变? - Why if I change background of my RelativeLayout the positions of the items of the Layout change? android为什么我的RelativeLayout无效 - android Why is my RelativeLayout not working 为什么setMargins无法与我的RelativeLayout一起使用? - Why is setMargins not working with my RelativeLayout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM