主线程与其他线程之间的Android通信(Android communication between main thread and other thread)
我想动态地改变textview的文本,但是如果我想制作一个游戏线程,我将需要相同的逻辑,所以我需要在主要的和第二个之间进行通信。
我有文件:
主要活动
public class MainActivity extends ActionBarActivity { public static Handler mHandler; Runnable thread = new SampleThread(); TextView txt1 = (TextView) findViewById(R.id.txt1); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActionBar actionBar = getSupportActionBar(); actionBar.hide(); //hiding status bar if (Build.VERSION.SDK_INT < 16) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } else { View decorView = getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); } setContentView(R.layout.activity_main); mHandler = new Handler() { public void handleMessage(Message msg) { // process incoming messages here // i want to change the text of txt1 here } }; new Thread(thread).start(); } }
SampleThread
package com.example.katsar0v.myapplication; import android.util.Log; /** * Created by Katsar0v on 1/21/2015. */ public class SampleThread implements Runnable { @Override public void run() { int two = 0; while(two<10) { two++; try { Thread.sleep(1000); //instead of logging, i want to send the text to main UI Log.d("MSG", String.valueOf(two + "sec")); } catch (InterruptedException e) { e.printStackTrace(); } } } }
我看到的问题是,当我的线程在另一个文件中时,如何用处理程序更改文本? 或者我应该在第一个类中使第二个类静态(当代码变得非常长时,我应该怎么做,它不能都在一个文件中)?
I want to change dynamically the text of a textview, but I will need the same logic if I want to make a game thread, so I need to make the communication between the main one and the second one.
I have the files :
MainActivity
public class MainActivity extends ActionBarActivity { public static Handler mHandler; Runnable thread = new SampleThread(); TextView txt1 = (TextView) findViewById(R.id.txt1); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActionBar actionBar = getSupportActionBar(); actionBar.hide(); //hiding status bar if (Build.VERSION.SDK_INT < 16) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } else { View decorView = getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); } setContentView(R.layout.activity_main); mHandler = new Handler() { public void handleMessage(Message msg) { // process incoming messages here // i want to change the text of txt1 here } }; new Thread(thread).start(); } }
SampleThread
package com.example.katsar0v.myapplication; import android.util.Log; /** * Created by Katsar0v on 1/21/2015. */ public class SampleThread implements Runnable { @Override public void run() { int two = 0; while(two<10) { two++; try { Thread.sleep(1000); //instead of logging, i want to send the text to main UI Log.d("MSG", String.valueOf(two + "sec")); } catch (InterruptedException e) { e.printStackTrace(); } } } }
The problem I see is, how do I change the text with the handler, when my thread is in another file? Or should I make the second class static within the first one (and what should I do when the code gets really long, it can't be all in one file)?
原文:https://stackoverflow.com/questions/28072858
最满意答案
给出要跳过的单选按钮:
class="jqTransformHidden"
,jqTransform将跳过这些按钮。AFAIK没有根据类型跳过元素的方法,只有类。
In the jQuery.jqTransform.js;
The line
if (selfForm.hasClass('ignore') == false) { $('input:radio', this).jqTransRadio(); }
was added to the Radio Button section.And
class="ignore"
onto<itemtemplate><asp:RadioButton CssClass="ignore" /></itemtemplate>
.All working okay now, the transform plugin just ignore's these inputs.
相关问答
更多-
每当更改单选按钮时,您必须删除其他LI元素上的样式 var lis = $('input[type="radio"]').change(function() { lis.css('border', 'none'); if ($(this).is(':checked') ) { $(this).parent().css('border', '1px solid black'); } }).parent(); // gets all the parent ...
-
jqtransform和折叠DIV(jqtransform and collapsed DIV)[2022-02-05]
我遇到了一个完全不同的问题,所以我想我会将此类解决方案与解决方案一起打印出来,以便人们可以希望找到它... jqtransform将z-index值应用于所有选择下拉列表(在本例中为元素,而不是选项),它从10开始向下执行。 这意味着第二个选择框的z-index将为9,第3,第8,第4,第7等等。如果超过11,则会进入负值。 如果您的选择框位于z-index值很重要的更复杂的div集(它们在背景中基本消失),这会导致很大的问题。 为了解决这个问题,修改jqtransform代码是一个好主意,因此它使用sel ... -
我这样做: $('input[name="rad"]').click(function() { $(this).closest('form').find('label').css('background', 'transparent') $(this).parent().css('background', 'red'); }); $('input[name="rad"]').click(function() { $(this).closest('form').find('label').cs ...
-
似乎原因是: 每个表单元素都有自己的包装div width在style属性中被硬编码(由js),所以在CSS中覆盖这个不起作用 jqtransform不提供元素ID,所以你必须手动设置它或者使用类来操作 所以,在你的情况下,我建议你使用一些jQuery: var myW = 300; $("#elementId").css("width", myW); $("#elementId").parents(".jqTransformInputWrapper").css("width", myW); Seems ...
-
尝试这个, $(".RadioClass").change(function(){ if($(this).is(":checked")) { var groupName = $(this).attr("name"); $(".RadioClass").each(function(idx) { if($(this).attr("name") == groupName) { $(this).next("label").rem ...
-
给出要跳过的单选按钮: class="jqTransformHidden" ,jqTransform将跳过这些按钮。 AFAIK没有根据类型跳过元素的方法,只有类。 In the jQuery.jqTransform.js; The line if (selfForm.hasClass('ignore') == false) { $('input:radio', this).jqTransRadio(); } was added to the Radio Button section. And class= ...
-
Bootstrap中的Bootstrap单选按钮没有样式不可更改(Bootstrap radio button in bootstrap without styling not changable)[2022-03-14]
问题在于代码中没有的部分。 我在单选按钮周围的div上有data-toggle =“button-radio”。 The problem was in the part that is not in the code. I had data-toggle="button-radio" on the div around the radio buttons. -
我通过执行以下操作触发选择列表的更改事件来修改Jqtransform.js文件,从而解决了这个问题。 实际上它是一个关于Jqtransform.js的bug 如果你得到我的这篇文章,你可能知道jqtransform是什么,而你可能正在努力进行下拉列表的更改事件。 它无法工作的原因是由于jqtransform使用无序列表创建了一个下拉列表,然后它隐藏了您的下拉列表。 修复其实很简单。 打开你的jquery.jqtransform.js文件,找到以下行: /* Fire the onchange event * ...
-
造型一个单选按钮(Styling a radio button)[2023-02-19]
造型(编辑:边框,颜色,背景)单选按钮本身不可能在CSS中。 但是你可以使用隐藏的单选按钮和覆盖图像来描述这里 http://code.stephenmorley.org/html-and-css/styling-checkboxes-and-radio-buttons/ http://www.andreapinchi.it/how-to-style-radio-buttons-with-pure-css/ 基本上你可以隐藏你的单选按钮,并在它的位置放置一个span ,当点击它时,它会改变它的样式: HTM ... -
如果你正在寻找一种风格的解决方案,我建议你坚持使用CSS。 JavaScript可以很好地控制行为等等,但在功能和速度方面都没有任何优于CSS样式的形式。 一些好的文章是Pretty Accessible Forms或者这些 。 I'd recommend sticking to CSS if you are merelly looking for a stylistic solution. JavaScript is good to control behaviours and the such, but ...