gpt4 book ai didi

java - 如何将 imageViews 设置为单击而不是双击

转载 作者:行者123 更新时间:2023-12-01 18:35:56 25 4
gpt4 key购买 nike

我的 Android 应用程序包含一个真/假游戏。用户应该单击真或假图像一次,然后就会出现下一条语句。我需要用户仅单击图像一次,但它只允许双击。

有没有办法将点击次数设置为单次而不是两次?我检查了触摸属性中的 focusable 和 focusable 为 null

<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) 将 View 设置为不可聚焦,因为如果它们可聚焦,则第一次触摸将“聚焦”它们(这对 ImageView 没有影响)并且第二个将调用 onClick() 方法。

如果设置为 false,则只需单击一下即可调用 onClick() 方法。

关于java - 如何将 imageViews 设置为单击而不是双击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60044465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com