<tfoot id='BwvTD'></tfoot>

  • <i id='BwvTD'><tr id='BwvTD'><dt id='BwvTD'><q id='BwvTD'><span id='BwvTD'><b id='BwvTD'><form id='BwvTD'><ins id='BwvTD'></ins><ul id='BwvTD'></ul><sub id='BwvTD'></sub></form><legend id='BwvTD'></legend><bdo id='BwvTD'><pre id='BwvTD'><center id='BwvTD'></center></pre></bdo></b><th id='BwvTD'></th></span></q></dt></tr></i><div id='BwvTD'><tfoot id='BwvTD'></tfoot><dl id='BwvTD'><fieldset id='BwvTD'></fieldset></dl></div>
    <legend id='BwvTD'><style id='BwvTD'><dir id='BwvTD'><q id='BwvTD'></q></dir></style></legend>
  • <small id='BwvTD'></small><noframes id='BwvTD'>

        • <bdo id='BwvTD'></bdo><ul id='BwvTD'></ul>

        Android Thread 修改 EditText

        时间:2023-10-01

        <i id='MmjEk'><tr id='MmjEk'><dt id='MmjEk'><q id='MmjEk'><span id='MmjEk'><b id='MmjEk'><form id='MmjEk'><ins id='MmjEk'></ins><ul id='MmjEk'></ul><sub id='MmjEk'></sub></form><legend id='MmjEk'></legend><bdo id='MmjEk'><pre id='MmjEk'><center id='MmjEk'></center></pre></bdo></b><th id='MmjEk'></th></span></q></dt></tr></i><div id='MmjEk'><tfoot id='MmjEk'></tfoot><dl id='MmjEk'><fieldset id='MmjEk'></fieldset></dl></div>
        <legend id='MmjEk'><style id='MmjEk'><dir id='MmjEk'><q id='MmjEk'></q></dir></style></legend>
        • <bdo id='MmjEk'></bdo><ul id='MmjEk'></ul>
            <tbody id='MmjEk'></tbody>

                <small id='MmjEk'></small><noframes id='MmjEk'>

                  <tfoot id='MmjEk'></tfoot>
                1. 本文介绍了Android Thread 修改 EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在线程启动的另一个函数中修改 EditText 时遇到问题:

                  I am having a problem with modifying EditText in another function started by the thread:

                  Thread thRead = new Thread( new Runnable(){
                      public void run(){
                         EditText _txtArea = (EditText) findViewById(R.id.txtArea);
                         startReading(_txtArea);
                      }
                   });
                  

                  我的功能如下:

                  public void startReading(EditText _txtArea){
                           _txtArea.setText("Changed");
                  }
                  

                  它总是在尝试修改编辑文本时强制关闭.有人知道为什么吗?

                  It always force closes while trying to modify the edittext. Does someone know why?

                  推荐答案

                  不应从非 UI 线程修改 UI 视图.唯一可以接触 UI 视图的线程是main"或UI"线程,即调用 onCreate()onStop() 和其他类似组件生命周期函数的线程.

                  UI views should not be modified from non-UI thread. The only thread that can touch UI views is the "main" or "UI" thread, the one that calls onCreate(), onStop() and other similar component lifecycle function.

                  因此,每当您的应用程序尝试从非 UI 线程修改 UI 视图时,Android 都会提前抛出异常以警告您这是不允许的.那是因为 UI 不是线程安全的,而这样的预警实际上是一个很棒的功能.

                  So, whenever your application tries to modify UI Views from non-UI thread, Android throws an early exception to warn you that this is not allowed. That's because UI is not thread-safe, and such an early warning is actually a great feature.

                  更新:

                  您可以使用 Activity.runOnUiThread() 来更新 UI.或者使用 AsyncTask.但是由于在您的情况下您需要不断地从蓝牙读取数据,因此不应使用 AsyncTask.

                  You can use Activity.runOnUiThread() to update UI. Or use AsyncTask. But since in your case you need to continuously read data from Bluetooth, AsyncTask should not be used.

                  这是 runOnUiThread() 的示例:

                  runOnUiThread(new Runnable() {            
                      @Override
                      public void run() {
                          //this will run on UI thread, so its safe to modify UI views.
                           _txtArea.setText("Changed");
                      }
                  });
                  

                  这篇关于Android Thread 修改 EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:RxTextView.textChanges 与 Edittext 上的 setText 下一篇:Android:将 EditText 限制为数字

                  相关文章

                  最新文章

                    • <bdo id='6NPS4'></bdo><ul id='6NPS4'></ul>
                  1. <i id='6NPS4'><tr id='6NPS4'><dt id='6NPS4'><q id='6NPS4'><span id='6NPS4'><b id='6NPS4'><form id='6NPS4'><ins id='6NPS4'></ins><ul id='6NPS4'></ul><sub id='6NPS4'></sub></form><legend id='6NPS4'></legend><bdo id='6NPS4'><pre id='6NPS4'><center id='6NPS4'></center></pre></bdo></b><th id='6NPS4'></th></span></q></dt></tr></i><div id='6NPS4'><tfoot id='6NPS4'></tfoot><dl id='6NPS4'><fieldset id='6NPS4'></fieldset></dl></div>
                  2. <legend id='6NPS4'><style id='6NPS4'><dir id='6NPS4'><q id='6NPS4'></q></dir></style></legend>

                    <small id='6NPS4'></small><noframes id='6NPS4'>

                    <tfoot id='6NPS4'></tfoot>