如何使用AlertDialog [Android]中的Intent启动网页?

时间:2016-12-22 18:31:45

标签: android android-intent dialog

如果检查了这三个项目中的一个,我如何设定网页? 我想它需要有ID,然后以某种方式使if语句确定选择哪一个。但它如何以及在何处实施?一些想法? :)

public class MainActivity extends Activity {

CharSequence[] items = {"First web site", "Second web site", "Third web site"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void dialog(View v){
    showDialog(0);
}

public void dialog1(View v){
    Toast.makeText(getBaseContext(), "No function.", Toast.LENGTH_LONG).show();
}

public void dialog2(View v){
    Toast.makeText(getBaseContext(), "No function.", Toast.LENGTH_LONG).show();
}

protected Dialog onCreateDialog(int id) {
    switch (id) {
        case 0:
            return new AlertDialog.Builder(this)

                    .setIcon(R.mipmap.ic_launcher)
                    .setTitle("Dialog with some text...")
                    .setSingleChoiceItems(items, 0, null)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(getBaseContext(), "OK pressed!", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setNegativeButton("Cancel", new     DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(getBaseContext(), "Cancel pressed!", Toast.LENGTH_SHORT).show();
                        }
                    }).create();
                }
        return null;
    }

}

2 个答案:

答案 0 :(得分:1)

您可以使用此方法使用Intent启动网页:

private void launchWebPage(String url)
{
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
}

并使用适当的onClickListener()从任意Button的{​​{1}}调用此方法。

答案 1 :(得分:1)

我认为这就是你要找的东西

    final CharSequence[] items = {"web1", "web2", "web3"};

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

    builder.setTitle("Title");
    builder.setSingleChoiceItems(items,0,null);
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int position) {
            ListView lw = ((AlertDialog)dialog).getListView();
            Object checkedItem = lw.getAdapter().getItem(lw.getCheckedItemPosition());
            Toast.makeText(getApplicationContext(),
                    ((String) checkedItem)
                    ,Toast.LENGTH_SHORT).show();
        }
    });

    builder.show();

`