• <small id='xqd89'></small><noframes id='xqd89'>

      <legend id='xqd89'><style id='xqd89'><dir id='xqd89'><q id='xqd89'></q></dir></style></legend>
      • <bdo id='xqd89'></bdo><ul id='xqd89'></ul>

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

      1. Android Thread 修改 EditText

        Android Thread modify EditText(Android Thread 修改 EditText)
      2. <i id='axCUY'><tr id='axCUY'><dt id='axCUY'><q id='axCUY'><span id='axCUY'><b id='axCUY'><form id='axCUY'><ins id='axCUY'></ins><ul id='axCUY'></ul><sub id='axCUY'></sub></form><legend id='axCUY'></legend><bdo id='axCUY'><pre id='axCUY'><center id='axCUY'></center></pre></bdo></b><th id='axCUY'></th></span></q></dt></tr></i><div id='axCUY'><tfoot id='axCUY'></tfoot><dl id='axCUY'><fieldset id='axCUY'></fieldset></dl></div>

          <tbody id='axCUY'></tbody>
        <legend id='axCUY'><style id='axCUY'><dir id='axCUY'><q id='axCUY'></q></dir></style></legend>

          <bdo id='axCUY'></bdo><ul id='axCUY'></ul>

              <tfoot id='axCUY'></tfoot>

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

                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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                  How to convert Integer to int?(如何将整数转换为整数?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)
                2. <tfoot id='9ncrs'></tfoot>

                        <bdo id='9ncrs'></bdo><ul id='9ncrs'></ul>

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

                            <small id='9ncrs'></small><noframes id='9ncrs'>