2014-01-29 42 views
1

好傢伙我試圖更新在短暫的延遲之後的UI,爲此我使用的處理器的postdelayed方法。以下代碼最初將我的textview文本設置爲「Processing」,並且包含在處理程序的runnable中的代碼得到執行,但不會更新UI?請注意,這是在做片段處理器postDelayed無法更新UI

TextView progressText=(TextView)parent.findViewById(R.id.inProgressText); 
progressText.setText("Processing"); 

getActivity().getMainLooper()).postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     TextView progressText=(TextView)parent.findViewById(R.id.inProgressText); 
     progressText.setText("Request completed");    
    } 
}, 3000); 

感謝您的幫助

+0

什麼錯碼? – pskink

+0

@pskink此代碼最初將我的TextView文本後面的「處理」,但多數民衆贊成代碼中包含的處理程序可運行得到執行,但犯規更新UI? – Adi

+0

難道你使用'parent.findViewById(R.id.inProgressText)一次;',下一次你使用'uiObject.findViewById(R.id.inProgressText);'? –

回答

1

使用此代碼

public class MainActivity extends Activity { 
TextView progressText = null; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.activity_main, container, false); 

    progressText = (TextView) rootView.findViewById(R.id.inProgressText); 
    progressText.setText("Processing"); 
    progressText.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      progressText.setText("Request completed"); 
     } 
    }, 3000); 
     return rootView; 
} 
} 
+0

拉詹沒有工作,請注意,此代碼片段是在片段 – Adi

+0

但你可以將其轉換成片段 – Rajan

+0

是香港專業教育學院已經實施了一個片段,沒有工作一樣。 – Adi

1

變化

getActivity().getMainLooper()).postDelayed(new Runnable() 

到:

final Handler handler = new Handler(); 

然後

handler.postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     final TextView progressText=(TextView)findViewById(R.id.inProgressText); 
     progressText.setText("Request completed");    
    } 
}, 3000); 
+0

與沒有工作,後來改變了我的代碼,你現在看到的是什麼一樣開始啓動。 – Adi

+0

「沒有工作」 - 這究竟意味着什麼? – Melquiades

0

請嘗試是這樣的:我在Fragement檢查,它可以正常使用。

View parent= inflater.inflate(R.layout.your_fragement, container, false); 
TextView progressText=(TextView)parent.findViewById(R.id.inProgressText); 
progressText.setText("Processing"); 

      progressText.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        progressText.setText("Request completed"); 
       } 
      }, 3000); 
+0

謝謝Mehul。 Rajan建議,但沒有解決。 – Adi