2017-12-11 8 views
0

毎回Stringを変更するスレッドループが必要です。 (例:d - > dd - > ddd - > dddd ...)私は、スレッドが実行されるだけでなく、ビューを更新することを期待しましたが、画面は最初のrun()実行( "dd")で停止します。なにが問題ですか?ありがとう!Android - 単純なスレッドループが機能しない

package com.example.name.app; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       thread t1 = new thread(); 
       t1.start(); 
      } 
     }); 
    } 

    private class thread extends Thread { 

     TextView textView = (TextView) findViewById(R.id.tv); 

     @Override 
     public synchronized void run() { 

      String text = "d"; 
      while (true) { 
       try { 
        text += "d"; 
        textView.setText(text); 
        sleep(5000); 
       } catch (Exception e) { 
        return; 
       } 
      } 
     } 
    } 

} 
+0

UI要素をUIスレッドで更新する必要があります。投稿するにはハンドラを使い、 'TextView'を更新するには' runOnUiThread'を使います。また、命名規則に従ってCamelCase for Classを使用してください。 – Chithra

答えて

1

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    TextView textView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     textView = (TextView) findViewById(R.id.tv); 
     final Button button = (Button) findViewById(R.id.btn); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       thread t1 = new thread(); 
       t1.start(); 
      } 
     }); 
    } 

    private class thread extends Thread { 

     String text = "d"; 

     @Override 
     public synchronized void run() { 

      while (true) { 
       try { 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          textView.setText(text); 
         } 
        }); 
        sleep(5000); 
        text += "d"; 
       } catch (Exception e) { 
        return; 
       } 
      } 
     } 
    } 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="digitdemo.com.myapplication.MainActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/tv" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Count" 
      app:layout_constraintBottom_toBottomOf="parent" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintTop_toTopOf="parent" /> 

     <Button 
      android:id="@+id/btn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Press" 
      app:layout_constraintBottom_toBottomOf="parent" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintTop_toTopOf="parent" /> 

    </LinearLayout> 

</android.support.constraint.ConstraintLayout> 

出力:

enter image description here

希望します。

+0

それは働く!!!!ありがとうございました:D私は今眠ることができます! –

+0

あなたの歓迎@ysdと正解としてマークしていただきありがとうございます。あなたをお手伝いします。コーディングを続けてください。 – InsaneCat

1

すべてのui要素はメインスレッドで処理されます。したがって、任意のui要素を更新するには、メインスレッド(UIスレッド)から行う必要があります。あなたは、UIスレッドに切り替えるためにrunOnUiThreadを使用することができます。

TextView textView; 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     textView = (TextView) findViewById(R.id.tv); 
     final Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       thread t1 = new thread(); 
       t1.start(); 
      } 
     }); 
    } 

private class thread extends Thread { 

     @Override 
     public synchronized void run() { 

      String text = "d"; 
      while (true) { 
       try { 
        text += "d"; 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          textView.setText(text); 
         } 
        }); 

        sleep(5000); 
       } catch (Exception e) { 
        return; 
       } 
      } 
     } 
} 
0

代わりのhandler.Handlerが異なる実行環境と同じメインスレッドで実行されるUIの使用をインクリメントして更新する簡単なタスクのためにスレッドクラスを拡張します。

簡単なハンドラを作成し、必要に応じてメインスレッドを更新します。

TextView textView = (TextView) findViewById(R.id.tv); 
String text = "d"; 

Runnable runnable=null; 
Handler newHandler=null; 

private void runTask() { 
    newHandler = new Handler(); 
    runnable = new Runnable() { 
     @Override 
     public void run() { 
      try { 
       //update UI 
       text += "d"; 
       textView.setText(text); 
       newHandler.post(runnable); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }; 
    newHandler.post(runnable); 
    } 
} 

とハンドラは、あなたが望むようthis..itが同じ出力が得られます試してみてください

newHandler.removeCallbacks(runnable); 
1

を使用停止します。

public class TestActivity extends AppCompatActivity { 
    TextView textView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_test); 
     textView = (TextView) findViewById(R.id.tv); 
     final Button button = findViewById(R.id.btn); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       thread t1 = new thread(); 
       t1.start(); 
      } 
     }); 
    } 

    private class thread extends Thread { 

     String text = "d"; 

     @Override 
     public synchronized void run() { 

      while (true) { 
       try { 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          textView.setText(text); 
         } 
        }); 
        sleep(5000); 
        text += "d"; 
       } catch (Exception e) { 
        return; 
       } 
      } 
     } 
    } 
関連する問題