从警报对话框按钮打开链接Android

时间:2015-05-04 07:20:43

标签: java android android-alertdialog

我有一个警告框,弹出询问用户帮助翻译两个按钮"帮助翻译"和"关闭"如何在用户点击"帮助翻译"它将他们带到一个网站example.com。

可以使用警告对话框完成此操作,还是必须使用自定义框

制作布局文件

还有我如何制作"帮助翻译"按钮向左和"关闭"右

enter image description here

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {

    //noinspection SimplifiableIfStatement
    case R.id.action_about:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher);
        builder.setTitle(getResources().getString(R.string.app_name));
        builder.setMessage(getResources().getString(R.string.about_text));
        builder.setNeutralButton("Close", null);
        builder.setCancelable(true);
        AlertDialog alert = builder.create();
        alert.show();
        return true;

        case R.id.action_translate:
            builder = new AlertDialog.Builder(this);
            builder.setIcon(R.drawable.ic_launcher);
            builder.setTitle(getResources().getString(R.string.app_name));
            builder.setMessage(getResources().getString(R.string.translate_text));
            builder.setPositiveButton ("Help Translate", null);
            builder.setNeutralButton("Close", null);
            builder.setCancelable(true);
            alert = builder.create();
            alert.show();
            return true;

2 个答案:

答案 0 :(得分:2)

onClickListner()PositiveButton实施NegativeButton

 builder.setNegativeButton("Close", new DialogInterface.OnClickListener()       {
      public void onClick(DialogInterface dialog, int whichButton) {
        // what ever you want to do with No option.
          builder.dismiss();
      }
    });

  builder.setPositiveButton("Help Translate", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      builder.dismiss();
      Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
      startActivity(browserIntent);
       }
    });

了解更多信息Go to

答案 1 :(得分:0)

你可以使用getButton(BUTTON_POSITIVE)获取Button;

http://developer.android.com/reference/android/app/AlertDialog.html#getButton(int)

然后,当您单击“帮助翻译”按钮时,您可以执行类似的操作:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);