2011-06-29 41 views
0
public class Talk extends Activity { 
private ProgressDialog progDialog; 
int typeBar; 
TextView text1; 
EditText edit; 
Button respond; 
private String name; 
private String textAtView; 
private String savedName; 

public void onCreate (Bundle savedInstanceState){ 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dorothydialog); 


    text1 = (TextView)findViewById(R.id.dialog); 
    edit = (EditText)findViewById(R.id.repsond); 
    respond = (Button)findViewById(R.id.button01); 

    respond.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      text1.setText("Welcome! Enter your name!"); 

      respond.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        name = edit.getText().toString(); 

        text1.setText("Cool! your name is "+name); 

       } 
      }); 

     } 
    }); 

} 

}如何保存狀態時,它的活動與摧毀這個

好了,所以我想弄清楚如何我會保存此活動的狀態。這只是我的代碼中的一小段代碼,向你們展示一個例子。所以我想保存狀態,當活動被破壞時,用戶會回到他們離開的地方。
第二件事,我想在每個按鈕單擊之間顯示一個快速的5秒進度對話框微調器。

+0

[此鏈接](http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle)解釋了Application/Activity生命週期及其回調方法,以及[this link](http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState)解釋了保存持久狀態。第二件事,你真的想要每個按鈕點擊之間的5秒進度對話框微調嗎?或者你寧願有5秒的時間用戶輸入被暫停? –

回答

1

對於第二件事

這應該工作:

public class TestActivity extends Activity implements Runnable, OnClickListener { 
private TextView tv; 
private ProgressDialog pd; 
private Button btn; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    tv = (TextView) this.findViewById(R.id.tv);  
    btn = (Button)findViewById(R.id.btn); 

    tv.setText("initial text"); 

    btn.setOnClickListener(this); 
} 

public void onClick(View v) { 
    pd = ProgressDialog.show(TestActivity.this, "Please wait...", "Details here", true, false); 

    Thread thread = new Thread(TestActivity.this); 
    thread.start(); 
} 
public void run() { 
    try { 
     Thread.sleep(5000); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 

    handler.sendEmptyMessage(0); 
} 

private Handler handler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     pd.dismiss(); 
     tv.setText("text after 5 sec passed"); 
    } 
}; 
} 
+0

謝謝!這樣可行! –

+0

現在我唯一需要的是在上面我的方法的textView中顯示文本之前顯示runDialog()。 –

+2

所以只需調用runDialog();之前textView.setText(); –