尝试从ClickListener更新时,TextView.setText没有显示在屏幕上

编程入门 行业动态 更新时间:2024-10-11 17:23:14
本文介绍了尝试从ClickListener更新时,TextView.setText没有显示在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人可以告诉我我在做什么错吗? 我有一个textView和一些按钮.当按下按钮时,我听到了激活该方法的按钮的监听器.在OnClick上下文中,新文本令人耳目一新,但从方法上讲,它不会在屏幕上显示新文本. setText不起作用.为什么? 这是代码

Hi, can any one tell me please what am I doing wrong? I''ve got a textView and some buttons. When pressing the button, I got a listener to the button that activating a method. while in the context of OnClick the new text is refreshing, from the methos it will not show the new text on the screen. setText is not working. Why? Here is the code

public class MyActivity extends Activity { private TextView tv_Dir; private TextView tv_File; private Button btn_start; private boolean fStart; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { //call the create fct. Of the base class super.onCreate(savedInstanceState); //load the layout specified in the layout.xml setContentView(R.layout.main); tv_Dir = (TextView)findViewById(R.id.editText_Dir); tv_File = (TextView)findViewById(R.id.editText_File); btn_start = (Button)findViewById(R.id.btn_Start); // then you assign the listener to button btn_start.setOnClickListener(btnListener); } // declare a OnClickListener that will execute different actions // depending on the view that was clicked View.OnClickListener btnListener = new View.OnClickListener(){ public void onClick(View view) { switch (view.getId()) { case R.id.btn_Start: try { if(fStart==false){ btn_start.setText("Stop"); fStart=true; String curPath = System.getProperty("user.dir"); //This is being print to the view and refresh. I can see it on screen tv_Dir.setText("Dir under check: " + curPath); tv_Dir.invalidate(); tv_Dir.refreshDrawableState(); //This is being print to the view and refresh. I can see it on screen tv_File.setText("File name:"); tv_File.invalidate(); tv_File.refreshDrawableState(); view.invalidate(); view.refreshDrawableState(); File [] roots = java.io.File.listRoots(); String strCrd = android.os.Environment.getExternalStorageState(); boolean bul = strCrd.equals(android.os.Environment.MEDIA_MOUNTED); if(bul){ for (int i=0; i< roots.length; i++){ int j = 0; j= scan_files(roots[i], view); } } } else{ //This is being print to the view and refresh. I can see it on screen view.getId(); btn_start.setText("Scan"); fStart=false; tv_Dir.setText("Dir: "); tv_Dir.invalidate(); tv_Dir.refreshDrawableState(); tv_File.setText("File: "); tv_File.invalidate(); tv_File.refreshDrawableState(); view.invalidate(); view.refreshDrawableState(); } } catch (Exception e) { tv_Dir.setText("dir Error"); view.invalidate(); view.refreshDrawableState(); throw new RuntimeException(e); } break; } } }; // if fail return 0 public int scan_files (File curFile, View view){ int ret = 0; if(fStart==false) return 2; try { //update Gui view.getId(); //!!!!!!!!! I can NOT see it on screen !!!!!!!!!!!!!! tv_Dir.setText("Current Dir: " + curFile.getCanonicalPath()+"/" + curFile.getName()); tv_Dir.invalidate(); tv_Dir.refreshDrawableState(); view.invalidate(); view.refreshDrawableState(); if(fStart==false) return 2; //create list File[] files = curFile.listFiles(); if(files==null){ return -1; } // Folder is empty else if (files.length == 0) { return -1; } // Folder contains files else { for(int i=0; i< files.length; i++){ if(files[i]!=null && files.length != 0) ret = 0; ret = scan_files(files[i],view); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } }

推荐答案

通常,您需要重绘/更新/绘制文本视图或周围区域. in general you''ll need to redraw/ update/ paint the textview or the surrounding area.

更多推荐

尝试从ClickListener更新时,TextView.setText没有显示在屏幕上

本文发布于:2023-08-06 14:38:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1313023.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:屏幕上   ClickListener   TextView   setText

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!