[英]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.