简体   繁体   English

Android中带有switch语句的AlertDialog

[英]AlertDialog with switch statement in android

I'm on the way of making an android app for my client for the first time. 我正在第一次为我的客户制作一个android应用。 He needs to implement multiple languages in the app. 他需要在应用程序中实现多种语言。 So, I put some relative layouts and while clicking it will be changed to the particular language. 因此,我放置了一些相对的布局,并在单击时将其更改为特定的语言。 But now, want to put an 'alert dialog' for selection confirmation. 但是现在,要放置一个“警报对话框”以进行选择确认。 For that, I made an alert box and implement a switch statement in the positive button. 为此,我制作了一个警报框,并在正面按钮中实现了switch语句。 But, it's not working and I'm not having any errors.:( This is my piece of code: 但是,它不起作用,并且我没有任何错误。:(这是我的代码:

rl_german.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        AlertDialog();; 

    }
});
rl_english.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

    AlertDialog();

    }
});
  }

 private void AlertDialog()
   {
        new AlertDialog.Builder(this)
        .setTitle("")
        .setMessage("Are you sure you want to change the language?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

 public void onClick(DialogInterface dialog, int which) { 
        switch(which){
        case R.id.rl_english:
            startActivity(new Intent(Language.this,HomeActivity2.class));   
        break;
        case R.id.rl_german:

        break;
        case R.id.rl_french:

        break;
    }
    }
 })
.setNegativeButton("No", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // do nothing
    }
 })
 .show();
} 

But, if I'm calling the 'startActivity' directly in my 'OnClick' method. 但是,如果我直接在“ OnClick”方法中调用“ startActivity”。 Its working fine. 它的工作正常。 Can anybody help me please. 有人可以帮我吗。 Thanks in advance :) 提前致谢 :)

The problem here is that which refers to the id of the Button clicked in the AllertDialog.Builder (eg BUTTON_POSITIVE , BUTTON_NEGATIVE ); 这里的问题是, which指的是id的的Button中点击AllertDialog.Builder (如BUTTON_POSITIVEBUTTON_NEGATIVE );

What you could do is make an Intent variable as a member variable (declared outside of a method) then call startActivity() with that variable as the parameter. 您可以做的是将Intent变量作为成员变量(在方法外部声明),然后使用该变量作为参数调用startActivity() Short example: 简短示例:

public class MyActivity extends Activity
{
     Intent i;

     public void onCreate(...)
     {
         ...
         rl_english.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
         i = new Intent(Language.this,HomeActivity2.class);
         AlertDialog();
      }

 private void AlertDialog()
{
    new AlertDialog.Builder(this)
    .setTitle("")
    .setMessage("Are you sure you want to change the language?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) { 
        startActivity(i));
}

This is quick untested code so you may need to adjust for your needs, add extra code and brackets, etc... The point is, that you are trying to compare which to the Button clicked in your Activity but this actually refers to the Button clicked in your AlertDialog . 这是未经测试的快速代码,因此您可能需要调整以适应自己的需要,添加额外的代码和方括号等。重点是,您正在尝试将which与“ Activity单击的Button进行比较which但这实际上是指Button在您的AlertDialog单击。 You need to instantiate the Intent depending on which Acitvity Button is clicked then start the Activity if the positive Button in your AlertDialog is clicked or cancel if the negative Button is clicked. 您需要根据单击哪个Acitvity Button来实例化Intent ,如果单击AlertDialog的正Button ,则启动Activity如果单击负Button则取消该AlertDialog

I think, the problem is, your switch is working on the base of "which" parameter coming from onClick(DialogInterface dialog, int which), but you are checking it with "R.id.rl_english". 我认为,问题是,您的开关是基于来自onClick(DialogInterface dialog,int which)的“哪个”参数工作的,但是您正在使用“ R.id.rl_english”对其进行检查。 Your none of the cases are matching the condition because "which" gives the button clicked not the id of any view. 您的情况都不符合条件,因为“哪”使按钮被单击而不是任何视图的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM