简体   繁体   中英

How to launch a webpage using Intent from a AlertDialog [Android]?

How can I intent a web page when one of those three item is checked? I suppose it needs to have ID and then somehow make if statement to determinate which one is chosen. But how and where can it be implemented? Some ideas? :)

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;
    }

}

You can launch a web page using Intent with this method:

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

And call this method from onClickListener() of any Button with appropriate URL .

I think this is what you are looking for

    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();

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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