如何将图像视图设置为单击而不是双击



我的安卓应用程序包含一个真/假游戏。 用户应单击真或假图像一次,将显示下一个语句。 我只需要用户单击图像一次,但它只允许双击。

有没有办法将点击设置为单次而不是两次? 我已经检查了触摸属性中的可聚焦和可聚焦是否为空

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#B5FFFFFF"
android:orientation="vertical"
tools:context=".industry_standards_questions">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.38"
android:background="#63FFFFFF">
<TextView
android:layout_width="420dp"
android:layout_height="150dp"
android:gravity="center"
android:text="Know your standards"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#0C40F1"
android:textSize="40dp"
android:textStyle="bold">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.38">
<TextView
android:id="@+id/statement_TV"
android:layout_width="420dp"
android:layout_height="150dp"
android:gravity="center"
android:text="Statement"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#020202"
android:textSize="20dp"
android:textStyle="bold">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.38"
android:background="#00FFFFFF">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:weightSum="2">
<ImageView
android:id="@+id/trueImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="20dp"
android:src="@drawable/true_button"></ImageView>
<ImageView
android:id="@+id/falseImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="20dp"
android:focusableInTouchMode="true"
android:src="@drawable/false_button"></ImageView>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.38">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical"
android:padding="20dp"
>

<Button
android:id="@+id/finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:text="Finish"
android:textColor="#050505"
android:background="#E4DABC53"
android:textSize="20dp"
android:textStyle="bold"
>
</Button>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>

package com.example.securityapp;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewAnimator;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.Collections;
public class industry_standards_questions extends AppCompatActivity {
DatabaseReference databaseUsers;
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseAuth mAuth;
TextView stmnt;
ImageView image_true, image_false;
Button points;
Statements tfStatements;
int statementsLength;
ArrayList<Item> statementList;
int currentStatement = 0;
int grade = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_industry_standards_questions);
databaseUsers = database.getReference();
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
stmnt = (TextView) findViewById(R.id.statement_TV);
image_true = (ImageView) findViewById(R.id.trueImage);
image_false = (ImageView) findViewById(R.id.falseImage);
tfStatements = new Statements();
statementsLength = tfStatements.tfStatements.length;
statementList = new ArrayList<>();
points = (Button) findViewById(R.id.final_score);
for(int i = 0; i < statementsLength; i++){
statementList.add(new Item(tfStatements.getStatement(i), tfStatements.getAnswer(i)));
}
Collections.shuffle(statementList);
setStmnt(currentStatement);

image_true.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkAnswer(currentStatement)) {
grade++;
if (currentStatement < statementsLength - 1)
setStmnt(currentStatement);
}
currentStatement++;
if(currentStatement >= statementsLength) { score(); }
}
});
image_false.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!checkAnswer(currentStatement)){
grade++;
if(currentStatement < statementsLength -1)
setStmnt(currentStatement);
}
currentStatement++;
if(currentStatement >= statementsLength) { score(); }
}
});
}
// show Statement
private void setStmnt(int number){
stmnt.setText(statementList.get(number).getStatement());
}
//check is answer is right
private boolean checkAnswer(int number){
String answer = statementList.get(number).getAnswer();
return answer.equals("true");
}
public void score(){
Users.currentUser.setScore(grade);
databaseUsers.child(Users.currentUserId).setValue(Users.currentUser);
Toast.makeText(this, "You scored " + grade + " points!", Toast.LENGTH_LONG).show();
grade = 0;
}
}

您可以将视图设置为无法使用view.setFocusable(false)进行聚焦,因为如果它们是可聚焦的,则第一次触摸将"聚焦"它们(对 ImageView 没有影响(,第二次触摸将调用 onClick((-方法。

如果它设置为 false,你只需要单击一下即可调用你的 onClick(( 方法。

最新更新