gpt4 book ai didi

Android:为什么onTouchListener() 会打开多个Alert Dialog?

转载 作者:太空狗 更新时间:2023-10-29 16:05:54 25 4
gpt4 key购买 nike

代码如下:

        textView1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
String content = textView1.getText().toString();
if (!content.equals("")){
showNameDialog();
}
return true;
}
});

很简单。如果字符串内容中有文本,它会执行 showNameDialog() 方法。

方法如下:

    private void showNameDialog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
dialogBuilder.setTitle(name.toString().toUpperCase());
dialogBuilder.setMessage("Name's frequency: " + arrayListToString);
dialogBuilder.setPositiveButton("ok", null);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
}

除了当我单击 textView1 时,它会打开两个、三个或四个 AlertDialogs 之外,一切都运行良好。为什么?我怎样才能让它只打开一个?

最佳答案

触摸不是点击,所以我假设在触摸 View 时可以多次调用 onTouch(先触摸,然后触摸,等等)。而是尝试使用 OnClickListener:

textView1.setClickable (true);
textView1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String content = textView1.getText().toString();
if (!content.equals("")){
showNameDialog();
}
}
});

关于Android:为什么onTouchListener() 会打开多个Alert Dialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15314689/

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