2013-07-17 76 views
1

我寫了下面的代碼,使用textview和按鈕顯示消息框。當按鈕點擊時,活動應該完成。但是這有時會起作用,並不一致。有時會掛起。如何使它始終如一地工作?請幫助完成()有時無法正常工作

public class MyClass extends Activity { 
private TextView mText; 
private Button mOk; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.msgbox); 

    mText = (TextView) findViewById(R.id.txt); 
    mText.setText(「Click ok to exit」); 

    mOk = (Button) findViewById(R.id.ok); 
    mOk.setOnClickListener(mOnOkClickListener); 
} 

private final OnClickListener mOnOkClickListener = new OnClickListener() { 
    public void onClick(View v) { 
     finish(); 
    } 
}; 

}

工作條件,在onPause,和的onStop被的onDestroy調用時單擊確定按鈕。掛起時,它只會調用onPause並掛起。如何使其一致工作?

+0

掛起時,它只會調用onPause並掛起。 ?並在這裏分享onpouse代碼 –

+0

你有沒有用onPause編寫代碼? –

+0

no..i只寫了這麼多的代碼 – indira

回答

1

您可以嘗試使用DialogFragment來表達您的訊息。
設置起來:

//imports you'll need to add: 
import android.app.Dialog; 
import android.app.AlertDialog; 
import android.support.v4.app.DialogFragment; 
import android.support.v4.app.FragmentActivity; 

//Activity that fires up your Dialog needs to be a FragmentActivity:   
public class MyActivity extends FragmentActivity { 

// ... 

//class representing Dialog you want to show 
public static class Dialog1 extends DialogFragment { 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setMessage("Click ok to close"); 
     builder.setPositiveButton("Button text", null); 
     return builder.create(); 
    } 
} 

執行你的對話框:

DialogFragment df = new Dialog1(); 
    df.show(getSupportFragmentManager(), "tag"); 

當然你也可以用這種方法創建更復雜的對話 - 我們只使用兩種方法從AlertDialog.Builder,但it has much more to offer

相關問題