这是我的家庭作业:
我无法检查是否同时点击了布局按钮。代码如下:
Java文件:
public class MainActivity extends Activity {
int a,b;
long lastDown;
long lastDuration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//startB = (Button) this.findViewById(R.id.button1); //- Divide rest of the screen in two blocks graphically (A and B) leaving some space in the bottom for a Big label box (left to right).
LinearLayout layout1 = (LinearLayout) this.findViewById(R.id.layout1);
LinearLayout layout2 = (LinearLayout) this.findViewById(R.id.layout2);
final Button btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
btn.setEnabled(false);
//btn.setText("keep one finger on each of the box and wait for count down to stop");
Toast.makeText(getBaseContext(),"keep one finger on each of the box and wait for count down to stop",
Toast.LENGTH_SHORT).show();
}
});
layout1.setOnTouchListener(new OnTouchListener() {
public int a;
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
Toast.makeText(getBaseContext(), "layout1", Toast.LENGTH_SHORT).show();
this.a=1;
return true;
}
else this.a=0;
return false;
}
});
layout2.setOnTouchListener(new OnTouchListener() {
public int b;
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
Toast.makeText(getBaseContext(), "layout2", Toast.LENGTH_SHORT).show();
this.b=1;
return true;
}
else this.b=0;
return false;
}
});
}
protected void checkclick() {
// TODO Auto-generated method stub
if(this.a==1 && this.b==1){
Toast.makeText(getBaseContext(), "both button is pressed", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getBaseContext(), "Click on the screen", Toast.LENGTH_SHORT).show();
}
}
}
XML代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" // divided in 2 equal parts layout which is clickable .
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="12dp"
android:text="Start" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:baselineAligned="false"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#FF0000"
android:clickable="true"
android:id="@+id/layout1"
>
<TextView
android:id="@+id/timer"
android:text="Time: "
android:paddingRight="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#00FF66"
android:clickable="true"
android:id="@+id/layout2"
>
//hope u can understand this layout .
</LinearLayout>
</LinearLayout>
<LinearLayout // To show label "Finger 1 in the box A took X seconds"
"Finger 2 in the box B took Y seconds"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:background="#FFAAFF" >
</LinearLayout>
</LinearLayout>
答案 0 :(得分:1)
这可以通过使用布尔标志来完成。
例如,
boolean btn1isclicked = false;
boolean btn2isclicked = false;
在onClick中,单击按钮后将布尔值更改为true。
要检查两个按钮是否都是点击,您可以执行类似
的操作 if(btn1isclicked == true && btn2isclicked == true){
dosomething();
}