Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何设置;ImageView";可点击或不可点击_Android_Kotlin - Fatal编程技术网

Android 如何设置;ImageView";可点击或不可点击

Android 如何设置;ImageView";可点击或不可点击,android,kotlin,Android,Kotlin,我正在做一个简单的记忆游戏。基本上,会出现一些ImageView,然后用户应该按照顺序单击ImageView。但是,排序时也可以单击ImageView。 因此,我希望排序未完成时,ImageView不可单击,然后我希望排序完成后,ImageView可以单击。我该怎么做 -编辑1 在您的解决方案之后,我在kotlin中尝试了image.setEnable=false方法,得到了结果 此处为相关章节 kotlin fun orderImages() { index=0 Collect

我正在做一个简单的记忆游戏。基本上,会出现一些
ImageView
,然后用户应该按照顺序单击
ImageView
。但是,排序时也可以单击
ImageView
。 因此,我希望排序未完成时,
ImageView
不可单击,然后我希望排序完成后,
ImageView
可以单击。我该怎么做

-编辑1 在您的解决方案之后,我在kotlin中尝试了
image.setEnable=false
方法,得到了结果

此处为相关章节
kotlin

fun orderImages() {
    index=0
    Collections.shuffle(controlArray)
    println(controlArray)
    runnable = object : Runnable {
        override fun run() {
            for (image in imageArray) {
                image.visibility = View.INVISIBLE
                image.isEnabled = false
            }
            if (index < controlArray.size) {
                imageArray[controlArray[index]].visibility = View.VISIBLE
                index++
            } else {
                handler.removeCallbacks(runnable)
                for (image in imageArray) {
                    image.visibility = View.VISIBLE
                    image.isEnabled = true

                }
            }
            handler.postDelayed(runnable, 1000)
        }
    }
    handler.post(runnable)
    userArray.clear()
}
fun orderImages(){
索引=0
Collections.shuffle(controlArray)
println(控制阵列)
runnable=对象:runnable{
覆盖趣味跑(){
用于(图像阵列中的图像){
image.visibility=View.INVISIBLE
image.isEnabled=false
}
if(索引
要以编程方式禁用单击侦听器,请使用
image.setEnabled(false)。然而,正如您所描述的,我认为您的排序功能是密集的计算,因此它需要很长的时间,如果时间是几秒钟,它可能会阻塞UI线程。因此,必须在新线程上运行排序函数。假设您的活动中有一个按钮
buttonStartSorting

final Handler handler = new Handler();
buttonStartSorting.setOnClickListener(new View.OnClickListener() {
    //disalbe listener for images
    for(imv in imageViews){
        imv.isEnable = false;
    }
    //do sort images on new thread
    Thread(Runnable {
        yourSortImagesMethod(imageViews);
        handler.post(Runable{
          //enable listener for images
          for(imv in imageViews){
             imv.isEnable = true;
          }
        })
    }).start()
})

实际上有一个
setClickable(boolean clickable)
方法,您可以在这里看到:

您只需在imageview上添加setOnClickListener(),并根据标记状态设置一个标记,使其可单击或不可单击

private boolean isSorted = false;
在代码中为图像列表添加视图的地方,使用“单击侦听器”-

holder.borrar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(isSorted) {
            //do what you want to do after it is sorted.
        }
    }
});
因此,通过这种方式,onClick将只在对图像进行排序的情况下执行任何操作。现在,在代码中的某个地方,类中应该有一个条件,这样标志的状态就会改变。所以会是这样的--


此代码块允许您启用或禁用布局中的所有视图

fun enableDisableViewGroup(viewGroup: ViewGroup, enabled: Boolean) {
            val childCount = viewGroup.childCount
            for (i in 0 until childCount) {
                val view = viewGroup.getChildAt(i) 
                    view.isEnabled = enabled 
                if (view is ViewGroup) {
                    enableDisableViewGroup(view, enabled)
                }
            }
        }

你为什么不在排序后设置你的点击侦听器?@FarukAcar等我几分钟我自己编辑代码时得到了正确的结果谢谢你我编辑了这篇文章,如果你对此有建议,你可以告诉我。
fun enableDisableViewGroup(viewGroup: ViewGroup, enabled: Boolean) {
            val childCount = viewGroup.childCount
            for (i in 0 until childCount) {
                val view = viewGroup.getChildAt(i) 
                    view.isEnabled = enabled 
                if (view is ViewGroup) {
                    enableDisableViewGroup(view, enabled)
                }
            }
        }