我是Android上的新手,但是有使用C#和c++的经验。
我只是试图显示每当按下一个按钮。我搜索发现,onClick事件无法做到这一点,因此我使用了onTouch事件。
对于第一个按钮,它很好并且有效。然后我添加了第二个和第三个,但是现在当我执行它时,应用程序崩溃了。
如果我删除带有事件的第二个和第三个按钮,应用程序仍然崩溃。在删除第一个Button及其事件之前,应用程序停止崩溃。
这是我的main.code和layout-xml的代码:
package com.android.kurve;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity{
boolean left=false;
boolean right=false;
final Button buttonLeft = (Button) findViewById(R.id.button1);
final Button buttonRight = (Button) findViewById(R.id.button2);
final Button buttonMove = (Button) findViewById(R.id.button3);
final TextView text = (TextView) findViewById(R.id.textView1);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonLeft.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
left=true;
text.setText("left");
} else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
left=false;
}
return true;
}
});
buttonRight.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
right=true;
text.setText("right");
} else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
right=false;
}
return true;
}
});
buttonMove.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
text.setText("move");
return true;
}
});
}
}
这里是布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="207dp"
android:text="Left" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/button1"
android:layout_marginRight="31dp"
android:text="Right" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="24dp"
android:layout_toLeftOf="@+id/textView1"
android:text="Move" />
</RelativeLayout>
谢谢您的帮助。
最佳答案
可能的问题是,您正在全局初始化布局元素,而不是在“onCreate”方法中对其进行初始化。创建 Activity 时将调用此方法,因此应在此处初始化所有布局行为或值。
关于android - OnTouch适用于1个按钮,但不适用于3个按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12874939/