如何仅在用户单击 AlertDialog 上的“确定”后才启动活动

发布于 2024-12-09 03:04:16 字数 1050 浏览 0 评论 0原文

我有一个 AlertDialog,我只想在单击“确定”后启动下一个活动。问题出在我的方法中,我的意图附加了局部变量,并且我无法放入 onCreateDialog 方法。

//local variable created
ArrayList<String> students = new ArrayList<String>();    
for(int i=0; i<studentList.size(); i++){
    students.add(studentList.get(i).getName());
}

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                     new ContextThemeWrapper(this, R.style.popup_theme));
alertDialog.setTitle("Select Game");           
alertDialog.setPositiveButton("3x3", new DialogInterface.OnClickListener() {  
public void onClick(DialogInterface dialog, int which) { 

    //I want the new Activity to start only after user clicks ok.
    Intent intent=new Intent(getApplicationContext(),NewActivity.class);
    //Local variable, undefined, cannot reference 
    intent.putStringArrayListExtra("students", students);
    startActivity(intent);
    finish();
    return;  
    }        
}); 
alertDialog.show();

有什么方法可以阻止 showDialog() 之后的代码运行,直到用户单击“确定”吗?我有需要将其放入意图中的局部变量。

I have an AlertDialog and I only want the next Activity to start after I have click "OK". The problem is in my method, I have local variables attached to my intent and I cannot put in the onCreateDialog method.

//local variable created
ArrayList<String> students = new ArrayList<String>();    
for(int i=0; i<studentList.size(); i++){
    students.add(studentList.get(i).getName());
}

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                     new ContextThemeWrapper(this, R.style.popup_theme));
alertDialog.setTitle("Select Game");           
alertDialog.setPositiveButton("3x3", new DialogInterface.OnClickListener() {  
public void onClick(DialogInterface dialog, int which) { 

    //I want the new Activity to start only after user clicks ok.
    Intent intent=new Intent(getApplicationContext(),NewActivity.class);
    //Local variable, undefined, cannot reference 
    intent.putStringArrayListExtra("students", students);
    startActivity(intent);
    finish();
    return;  
    }        
}); 
alertDialog.show();

Is there any way I can prevent the code after showDialog() from running until the user clicks OK? I have local variable that I need to put inside the intent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

将军与妓 2024-12-16 03:04:16

使用此代码

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                         new ContextThemeWrapper(this, R.style.popup_theme));
    alertDialog.setTitle("Select Game");         
    alertDialog.setMessage("[ Choose Puzzle Matrix ]");      
    alertDialog.setPositiveButton("3x3", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int which) { 
        Intent myIntent = new Intent(((Dialog) dialog).getContext(), Matrix3x3.class);
        startActivity(myIntent);    
        return;  
        }        
    });      
    alertDialog.show();

use this code

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                         new ContextThemeWrapper(this, R.style.popup_theme));
    alertDialog.setTitle("Select Game");         
    alertDialog.setMessage("[ Choose Puzzle Matrix ]");      
    alertDialog.setPositiveButton("3x3", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int which) { 
        Intent myIntent = new Intent(((Dialog) dialog).getContext(), Matrix3x3.class);
        startActivity(myIntent);    
        return;  
        }        
    });      
    alertDialog.show();
颜漓半夏 2024-12-16 03:04:16

students 设为 final 变量。

final ArrayList<String> students = new ArrayList<String>();

Make students variable final.

final ArrayList<String> students = new ArrayList<String>();
无人接听 2024-12-16 03:04:16

不确定这是否是正确的方法,但我通过将 students 设为全局变量来进行“黑客攻击”。欢迎更好的答案。

Not sure if this is a right way but I did a 'hack' by making students a global variable. Would welcome better answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文