我正试图向Kutton展示一个信息箱。 为此,我使用了警示组织,我认识到,它并不阻碍守则的实施。 例:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 1")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
})
.setNegativeButton("", null)
.show();
new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 2")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
})
.setNegativeButton("", null)
.show();
//...continue with UI initialization here...
}
When I start activity, it shows Alert2, When I press ok it shows Alert1 afterwards.
我需要阻止密码的辩词,因此首先应当显示警示1的信息,直到用户继续实施《科索沃报》和显示警2信息等。 例:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
msgBox("Test dlg", "Alert 1");
msgBox("Test dlg", "Alert 2");
//...continue with UI initialization here...
}
private void msgBox(String title, String msg){
//?????
/* WRONG, NON-BLOCKING
new AlertDialog.Builder(this).setTitle(title).setMessage(msg)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
})
.setNegativeButton("", null)
.show();
*/
}
我应该写什么? 页: 1 方框方法?
我需要这样的东西:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isInitialisationDataFailed()){
msgBox("Alert", "Cannot open activity, sorry");
myActivity.this.finish();
return;
}
}
但这并不可行。 活动结束比屏幕上显示的警示更快。
其主要想法是,信息箱代码要单独使用,以掌握可加以利用的方法。 如何做到这一点?
///////////////////////////////// another example:
private void init(){
//init code here...
if (isSomethingWhrong()){
msgbox("wrong stuff will be fixed");
//do fix wrong stuff here...
}
if (isAnotherthingWrong()){
msgbox("more wrong stuff will be fixed");
//do fix more wrong stuff....
}
//continue init code here...
}
private void msgbox(String msg){
//BLOCKING DIALOG REALISATION here...
}
并作为替代办法:
private void init(){
//init code here...
handleWrongStuff();
}
private void handleWrongStuff(){
if (isSomethingWhrong()){
new AlertDialog.Builder(activity)
.setTitle("Test")
.setMessage("wrong stuff will be fixed")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do fix wrong stuff here...
handleMoreWrongStuff();
}
})
.setNegativeButton("", null)
.show();
}
else{
handleMoreWrongStuff();
}
}
private void handleMoreWrongStuff(){
if (isAnotherthingWrong()){
new AlertDialog.Builder(activity)
.setTitle("Test")
.setMessage("more wrong stuff will be fixed")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do fix more wrong stuff here...
continueInit();
}
})
.setNegativeButton("", null)
.show();
}
else{
continueInit();
}
}
private void continueInit(){
//continue init code here...
}
do you see the difference in complexity? In order to make init code working in Android I need to split it to separate methods but not on logical blocks but when I need to show dialogs. Moreover the code for initialization dialogs are repeated and became ugly and unreadable.