繁体   English   中英

为操作1选择的OnOptionsItem不显示新的警报对话框

[英]OnOptionsItemSelected for action 1 not showing new alert dialog

当用户点击操作设置1时,在我的onOptionsItemSelected方法上,我的新警报对话框不显示吗?

package com.karanvir.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import static com.karanvir.search.MainActivity.urlGlobal;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //finding webview
        WebView webView=(WebView) findViewById(R.id.web1);

//whole bunch of settings you might want to do if you .
        //do this because javascript is so wildely used that if you dont use this anywebsites you display wont be displayed properly
        webView.getSettings().setJavaScriptEnabled(true);
        //this is because on a number of phones if you dont do this it will jump to the devices default browser, and ddisplay the websview their instead.
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("https://www.google.ca/?gws_rd=ssl#q="+urlGlobal);
        //reminder ask permission
        //you can load content using loaddata
        //then add type of data
        //then add character encoded were using

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @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.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings4) {
            Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
            startActivity(intentGoogle);



            return true;
        } else if(id ==R.id.action_settings2){
            Intent intentGoogle= new Intent(getApplicationContext(),Yahoo.class);
            startActivity(intentGoogle);


            return true;

        }else if (id==R.id.action_settings3){
            Intent intentGoogle= new Intent(getApplicationContext(),MainActivity.class);
            startActivity(intentGoogle);


            return true;

        }else if(id==R.id.action_settings1){
            new AlertDialog.Builder(getApplicationContext())
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setTitle("About")
                    .setMessage("This app interchanges between different search engines, to help guess what your looking for. We dont not acess your location, also we do not save any information. Right now we are working on a way to make the app completely private in congnito.Email us if you want to help with this project also. Please email Dhillonapps93@gmail.com for help/inquries. We run this app with no ads, we also accept donations at runescapegold1291@hotmail.com. Yes I used to play runescape lol. Thanks for reading and enjoy");
            return true;

        }

        return super.onOptionsItemSelected(item);
    }

 }

您忘了在新对话框中调用.show()。 您只创建了它;)

else if(id==R.id.action_settings1){
    new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_info)
            .setTitle("About")
            .setMessage(".....")
            .show();
    return true;
}

您只需要编写alertDialog.show(); 显示对话框……示例如下

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            getActivity());
    // Setting Dialog Title
    alertDialog.setTitle("Title is here");

    // Setting Dialog Message
    alertDialog.setMessage("Message is here");

    // On pressing ok button
    alertDialog.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                     //do whatever you want

                    dialog.cancel(); //it cancel the alertdialog
                }
            });
            //on pressing cancel button
    alertDialog.setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                    //do whatever you want

                    dialog.cancel(); //it cancel the alertdialog
                }
            });
            // Showing Alert Message
    alertDialog.show();
    alertDialog.setCancelable(false);

暂无
暂无

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

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