Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.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 更改文本时遇到问题查看按钮上的文本单击_Android - Fatal编程技术网

Android 更改文本时遇到问题查看按钮上的文本单击

Android 更改文本时遇到问题查看按钮上的文本单击,android,Android,我的应用程序中有一个按钮和一个文本视图。单击按钮之前,textview文本始终为“开始搜索”。但单击按钮之后,将调用函数(scanning())。执行函数需要一些时间。因此,我希望texview文本在单击按钮之后和获得函数结果之前的时间段内为“请稍候”。得到函数结果后,textview文本将更改为“已找到”或“未找到” 但问题是,它从不显示“请稍候”。点击按钮后显示“开始搜索”,直到得到函数结果 点击按钮后,如何在文本视图上显示“请等待”,直到它从函数中获得结果 Button b

我的应用程序中有一个按钮和一个文本视图。单击按钮之前,textview文本始终为“开始搜索”。但单击按钮之后,将调用函数(scanning())。执行函数需要一些时间。因此,我希望texview文本在单击按钮之后和获得函数结果之前的时间段内为“请稍候”。得到函数结果后,textview文本将更改为“已找到”或“未找到”

但问题是,它从不显示“请稍候”。点击按钮后显示“开始搜索”,直到得到函数结果

点击按钮后,如何在文本视图上显示“请等待”,直到它从函数中获得结果

        Button button = (Button) findViewById(R.id.search);
        TextView text = (TextView) findViewById(R.id.searchR);
        text.setText("Start Search");
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                text.setText("Please Wait");
                boolean res=scanning();
                if(res==true)
                text.setText("Found");
                else text.setText("Not Found");
            }
        });
XML部分:

<Button
    android:id="@+id/search"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="60dp"
    android:layout_marginLeft="60dp"
    android:layout_marginTop="30dp"
    android:text="Search"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/searchR"
    android:layout_width="0dp"
    android:layout_height="34dp"
    android:layout_marginStart="97dp"
    android:layout_marginLeft="97dp"
    android:layout_marginTop="30dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/search"
    app:layout_constraintTop_toTopOf="parent" />


在扫描部分,我使用UUID搜索了一些BLE设备。如果找到该设备,则我返回true,否则返回false

我在你的代码中发现了一些错误

  • 您正在更改按钮的文本,而不是文本视图

        public void onClick(View v) {
    
            text.setText("Please Wait");
            boolean res=scanning();
            if(res==true)
            but1.setText("Found");
            else but1.setText("Not Found");
        }
    
  • 这里

    应替换为

    text.setText
    
  • 您的扫描方法返回结果的速度非常快,这就是为什么您无法获得请等待文本的原因

  • 在将文本更改为“请稍候”后和调用
    scanning()
    之前引入一个小延迟


    编辑:根据您的代码,您可能需要将变量声明为静态变量或全局变量,以使其工作。

    为更改文本编写外部方法, 例如:

            public void onClick(View v)
            {
                changeText("Please Wait");
                boolean res = scanning();
                if (res) // (res == true) is unnecessary, you can use like this
                    changeText("Found");
                else changeText("Not Found");
            }
    
            private void changeText(string s)
            {
                text.setText(s);
            }
    

    在使用TextView和按钮的位置也共享xml文件请共享
    scanning()
    调试代码。若您看到一切正常,那个么XML文件中一定缺少一些内容。如果你分享,我们可以说我已经分享了XML的一部分!对不起,误会了!在我的申请中,文本是but1。但是在分享我的代码之前,我把它改成了文本。但不幸的是,其中一些丢失了。@mahbucseju共享您的扫描方法。虽然您正在使用另一个100毫秒的等待,但它工作正常!如果愿意,可以减少毫秒数。50毫秒或更少也可以达到效果。
    button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
    
                    text.setText("Please Wait");
    
                    Handler delayHandler = new Handler();
                    delayHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            boolean res=scanning();
                            if(res) {
                                text.setText("Found");
                            }
                            else{
                                text.setText("Not Found");
                            }
                        }
                    },100);
                }
            });
    
            public void onClick(View v)
            {
                changeText("Please Wait");
                boolean res = scanning();
                if (res) // (res == true) is unnecessary, you can use like this
                    changeText("Found");
                else changeText("Not Found");
            }
    
            private void changeText(string s)
            {
                text.setText(s);
            }