尝试从ClickListener更新时,TextView.setText没有显示在屏幕上 [英] TextView.setText is not showing on screen while trying to update from a ClickListener

查看:68
本文介绍了尝试从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没有显示在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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