Android Thread modify EditText

后端 未结 5 2164
一向
一向 2021-01-14 04:34

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

Thread thRead = new Thread( new Runnable(){
    public void run(){
          


        
5条回答
  •  滥情空心
    2021-01-14 05:02

    By default, the main thread is the UI thread. All code that modifies the appearance of the application needs to be run in this thread. If you want to have multiple threads in your application that can modify the UI I would suggest using the AsyncTask class.

    public someMethod(){
        new ChangeTextTask().execute();
    }
    
    private class ChangeTextTask extends AsyncTask {
        @Override
        protected Void doInBackground(Void... params) {
            startReading(_txtArea);
            return null;
        }
    }
    

    However, you need to take steps to prevent multiple threads from accessing the EditText object at once. Otherwise you'll wind up getting a CurrentModificationException error.

    http://developer.android.com/reference/android/os/AsyncTask.html

提交回复
热议问题