开始buttonclick新线程 [英] Start new thread on buttonclick

查看:204
本文介绍了开始buttonclick新线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的按钮启动一个新的线程,并摧毁该按钮位于之一,在单击它,我怎么做呢?

 包inno.games;进口android.app.Activity;
进口android.content.Context;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.inputmethod.InputMethodManager;
进口android.widget.Button;
进口android.widget.EditText;公共类Introscreen延伸活动{
按钮继续;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.intro);
继续=(按钮)findViewById(R.id.bProceed);}


解决方案

  

破坏[主题]上的按钮位于


虽然你的写法是不完全清楚,只有一个UI线程是主线程。该按钮的UI是一部分坐落在UI线程(或多或少)。你可以不破坏该线程,并取代它。

如果有具体的,你要找这个实现的东西,你应该张贴的具体要求一起。

更新:

如果你只是想在技术上再这样做就创建点击一个新的线程:

  proceed.setOnClickListener(新OnClickListener(){
    @覆盖
    公共无效的OnClick(视图v){
        新的Thread();
    }
});

但是,这并不确切做任何有用的。还要注意的是创建一个新的线程不会得到的CPU使用率降到最低。创建一个新的线程创建的更多为CPU做的工作。然而,你也许能够从UI线程一些工作移到后台线程这将使得用户体验更加流畅。如果这是你的目标,你应该阅读有关本文档无痛线程

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);



}

解决方案

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.

这篇关于开始buttonclick新线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆