alertdialog重定向到url

时间:2014-09-16 06:09:30

标签: android

我使用了android alertdialog以便重定向到一个url,重定向应该根据用户的选择,这里是代码:

   final CharSequence[]stringArray =  {"1" ,"2" , "3"};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Press selected name");
    builder.setItems(stringArray, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
            String st2 = "https://www.youtube.com/";
            if (stringArray.toString().equals("1")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, 
                         Uri.parse(st));
                    startActivity(intent);
            }
            else if (stringArray.toString().contains("2")) {                

                    Intent intent = new Intent(Intent.ACTION_VIEW, 
                             Uri.parse(st2));
                        startActivity(intent);                  
                                    }
        }
    });
    AlertDialog alert = builder.create();
    alert.show();

但是当我点击1或2时,没有重定向到网址 代码出了什么问题?

2 个答案:

答案 0 :(得分:0)

您正在检查数组项目,这是无效的实施方式。您需要检查所选项目的ID,您只能通过onclick方法获取该ID。

由于数组计数从“0”开始,因此可以使用“0到2”检查所选项目,其中0将被视为“1”,依此类推。

查看以下代码:

    final CharSequence[] stringArray = { "1", "2", "3" };
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Press selected name");
    builder.setItems(stringArray, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
            String st2 = "https://www.youtube.com/";
            if (item == 0)
            // if (stringArray.toString().equals("1"))
            {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                        .parse(st));
                startActivity(intent);
            } else if (item == 1)
            // else if (stringArray.toString().contains("2"))
            {

                Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                        .parse(st2));
                startActivity(intent);
            }
        }
    });
    AlertDialog alert = builder.create();
    alert.show();

答案 1 :(得分:0)

请使用以下代码: -

final CharSequence[] stringArray = { "1", "2", "3" };

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Press selected name");

builder.setItems(stringArray, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int position) {

String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";

String st2 = "https://www.youtube.com/";

if (position == 0) {

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(st));

startActivity(intent);

} 

else if (position == 1) {

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(st2));

startActivity(intent);

} 

else if (position == 2) {

// write some code

}

}

});

AlertDialog alert = builder.create();

alert.show();