Start new thread on buttonclick

2019-08-04 05:53发布

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条回答
家丑人穷心不美
2楼-- · 2019-08-04 06:13

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.

查看更多
登录 后发表回答