Start new thread on buttonclick

无人久伴 提交于 2019-12-24 10:38:07

问题


I want my button to start up a new thread and destroy the one on which the button is located, upon clicking it, how do i do this?

package inno.games;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class Introscreen extends Activity {


Button proceed;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.intro);


proceed = (Button) findViewById(R.id.bProceed);



}

回答1:


destroy the [thread] on which the button is located

Although your wording is not exactly clear, there is only one UI thread which is the main thread. The button being part of the UI "is located" (more or less) on the UI thread. You can't destroy that thread and replace it.

If there's something specific that you're looking to achieve by this, you should post that along with specific requirements.

Update:

If you just want to create a new thread on click then technically this does it:

proceed.setOnClickListener(new OnClickListener() {
    @Override
    public void OnClick(View v) {
        new Thread();
    }
});

but that doesn't exactly do anything useful. Also note that creating a new thread does not "get the CPU usage to a minimum". Creating a new thread creates more work for the CPU to do. However, you might be able to move some work from the UI thread to a background thread which will make the user experience smoother. If that's your goal, you should probably read this document entitled Painless Threading.



来源:https://stackoverflow.com/questions/9404456/start-new-thread-on-buttonclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!