使用菜单时无法使用WebView打开应用程序

时间:2019-05-21 05:59:16

标签: webview

任何帮助或提示将不胜感激。 我可以使用以下WebView本身在android中打开Web应用程序,但是当我尝试将Webview与菜单结合使用时,似乎不起作用,

有效的MainActivity代码:                 包com.example.app;

            import android.annotation.SuppressLint;
            import android.app.Activity;
            import android.os.Bundle;
            import android.os.Message;
            import android.webkit.WebSettings;
            import android.webkit.WebView;
            import android.webkit.WebViewClient;

            import java.net.URLEncoder;

            public class MainActivity extends Activity {

                private WebView mWebView;

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

                mWebView = findViewById(R.id.activity_main_webview);

                // Force links and redirects to open in the WebView instead of in a browser
                mWebView.setWebViewClient(new WebViewClient());

                // Enable Javascript
                WebSettings webSettings = mWebView.getSettings();
                webSettings.setJavaScriptEnabled(true);

                // REMOTE RESOURCE

                try {




                    String html = "<!DOCTYPE html>" +
                        "<html>" +
                        "<head>" +
                        "<script>" +
                        "function submitData(){ "+
                        "document.form1.submit();"+
                        "}" +
                        "</script>" +
                        "</head>" +
                        "<body onload='submitData()'> "+
                        "<form action=\"https://www.test.ca\" method='post' name='form1'>\n"+
                        "<input type='hidden' name='user' value='test.ca'><br/>" +
                        "<input type='hidden' name='password' value='test1'><br>" +
                        "</form>" +
                        "</body>" +
                        "</html>";

                    mWebView.loadData(html, "text/html", "UTF-8");

                     html = "<!DOCTYPE html>" +
                        "<html>" +
                        "<head>" +
                        "<script>" +
                        "function submitData(){ "+
                        "document.form1.submit();"+
                        "}" +
                        "</script>" +
                        "</head>" +
                        "<body onload='submitData()'> "+

                        "<form action=\"https://www.test.ca/pages/recordList.cfm?list=Allergy\" method='post' name='form1'>\n"+

                        "</form>" +
                        "</body>" +
                        "</html>";

                    mWebView.loadData(html, "text/html", "UTF-8");



                }
                catch(Exception e)
                {
                    System.out.println("error message:"+e.getMessage());
                }

                }

                // Prevent the back-button from closing the app
                @Override
                public void onBackPressed() {
                if(mWebView.canGoBack()) {
                    mWebView.goBack();
                } else {
                    super.onBackPressed();
                }
                }

            }

但是,与菜单结合使用时,以下Web视图不起作用:

            import android.os.Bundle;
            import android.support.annotation.NonNull;
            import android.support.design.widget.FloatingActionButton;
            import android.support.design.widget.Snackbar;
            import android.util.Log;
            import android.view.View;
            import android.support.design.widget.NavigationView;
            import android.support.v4.view.GravityCompat;
            import android.support.v4.widget.DrawerLayout;
            import android.support.v7.app.ActionBarDrawerToggle;
            import android.support.v7.app.AppCompatActivity;
            import android.support.v7.widget.Toolbar;
            import android.view.Menu;
            import android.view.MenuItem;
            import android.webkit.WebView;
            import android.widget.ExpandableListView;

            import java.util.ArrayList;
            import java.util.HashMap;
            import java.util.List;

            import android.webkit.WebSettings;

            public class MainActivity extends AppCompatActivity
                implements NavigationView.OnNavigationItemSelectedListener {


                ExpandableListAdapter expandableListAdapter;
                ExpandableListView expandableListView;
                List<MenuModel> headerList = new ArrayList<>();
                HashMap<MenuModel, List<MenuModel>> childList = new HashMap<>();

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

                FloatingActionButton fab = findViewById(R.id.fab);
                fab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                    }
                });

                expandableListView = findViewById(R.id.expandableListView);
                prepareMenuData();
                populateExpandableList();

                DrawerLayout drawer = findViewById(R.id.drawer_layout);
                ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                drawer.addDrawerListener(toggle);
                toggle.syncState();

                NavigationView navigationView = findViewById(R.id.nav_view);
                navigationView.setNavigationItemSelectedListener(this);
                }

                @Override
                public void onBackPressed() {
                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else {
                    super.onBackPressed();
                }
                }

                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.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_settings) {
                    return true;
                }

                return super.onOptionsItemSelected(item);
                }

                @SuppressWarnings("StatementWithEmptyBody")
                @Override
                public boolean onNavigationItemSelected(MenuItem item) {
                // Handle navigation view item clicks here.
                int id = item.getItemId();

                if (id == R.id.nav_camera) {
                    // Handle the camera action
                } else if (id == R.id.nav_gallery) {

                } else if (id == R.id.nav_slideshow) {

                } else if (id == R.id.nav_manage) {

                } else if (id == R.id.nav_share) {

                } else if (id == R.id.nav_send) {

                }

                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                drawer.closeDrawer(GravityCompat.START);
                return true;
                }

                private void prepareMenuData() {



                MenuModel menuModel = new MenuModel("PERSONAL RECORDS", true, true, ""); //Menu of Java Tutorials
                headerList.add(menuModel);
                List<MenuModel> childModelsList = new ArrayList<>();
                MenuModel childModel = new MenuModel("ALLERGIES", false, false, "https://www.xxx.ca/pages/recordList.cfm?list=Allergy");
                childModelsList.add(childModel);

                childModel = new MenuModel("ADVANCE CARE PLANNING", false, false, "https://www.journaldev.com/19187/java-fileinputstream");
                childModelsList.add(childModel);

                childModel = new MenuModel("CONDITIONS", false, false, "https://www.journaldev.com/19115/java-filereader");
                childModelsList.add(childModel);

                childModel = new MenuModel("DIARY", false, false, "https://www.journaldev.com/19115/java-filereader");
                childModelsList.add(childModel);


                childModel = new MenuModel("IMMUNIZATIONS", false, false, "https://www.journaldev.com/19115/java-filereader");
                childModelsList.add(childModel);

                childModel = new MenuModel("MEASUREMENTS", false, false, "https://www.journaldev.com/19115/java-filereader");
                childModelsList.add(childModel);

                childModel = new MenuModel("MEDICATIONS", false, false, "https://www.journaldev.com/19115/java-filereader");
                childModelsList.add(childModel);


                if (menuModel.hasChildren) {
                    Log.d("API123","here");
                    childList.put(menuModel, childModelsList);
                }

                menuModel = new MenuModel("CLINICAL RECORDS", true, false, "https://www.journaldev.com/9333/android-webview-example-tutorial"); //Menu of Android Tutorial. No sub menus
                headerList.add(menuModel);

                childModelsList = new ArrayList<>();
                childModel = new MenuModel("TEST CLINICAL RECORDS", false, false, "https://www.journaldev.com/7153/core-java-tutorial");
                childModelsList.add(childModel);

                if (!menuModel.hasChildren) {
                    childList.put(menuModel, childModelsList);
                }

                childModelsList = new ArrayList<>();
                menuModel = new MenuModel("MESSAGES", true, true, ""); //Menu of Python Tutorials
                headerList.add(menuModel);
                childModel = new MenuModel("INBOX", false, false, "https://www.journaldev.com/19243/python-ast-abstract-syntax-tree");
                childModelsList.add(childModel);

                childModel = new MenuModel("SENT", false, false, "https://www.journaldev.com/19226/python-fractions");
                childModelsList.add(childModel);

                if (menuModel.hasChildren) {
                    childList.put(menuModel, childModelsList);
                }
                }

                private void populateExpandableList() {

                expandableListAdapter = new ExpandableListAdapter(this, headerList, childList);
                expandableListView.setAdapter(expandableListAdapter);

                expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                    @Override
                    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

                    if (headerList.get(groupPosition).isGroup) {
                        if (!headerList.get(groupPosition).hasChildren) {
                        WebView webView = findViewById(R.id.webView);
                        webView.loadUrl(headerList.get(groupPosition).url);
                        onBackPressed();
                        }
                    }

                    return false;
                    }
                });

                expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

                    if (childList.get(headerList.get(groupPosition)) != null) {
                        MenuModel model = childList.get(headerList.get(groupPosition)).get(childPosition);
                        if (model.url.length() > 0) {
                        WebView webView = findViewById(R.id.webView);

                        // Enable Javascript
                        WebSettings webSettings = webView.getSettings();
                        webSettings.setJavaScriptEnabled(true);

                        String html = "<!DOCTYPE html>" +
                            "<html>" +
                            "<head>" +
                            "<script>" +
                            "function submitData(){ "+
                            "document.form1.submit();"+
                            "}" +
                            "</script>" +
                            "</head>" +
                            "<body onload='submitData()'> "+
                            "<form action=\"https://www.test.ca\" method='post' name='form1'>\n"+
                            "<input type='hidden' name='user' value='test.ca'><br/>" +
                            "<input type='hidden' name='password' value='test'><br>" +
                            "</form>" +
                            "</body>" +
                            "</html>";

                        webView.loadData(html, "text/html", "UTF-8");

                        html = "<!DOCTYPE html>" +
                            "<html>" +
                            "<head>" +
                            "<script>" +
                            "function submitData(){ "+
                            "document.form1.submit();"+
                            "}" +
                            "</script>" +
                            "</head>" +
                            "<body onload='submitData()'> "+

                            "<form action=\"https://www.test.ca/pages/recordList.cfm?list=Allergy\" method='post' name='form1'>\n"+

                            "</form>" +
                            "</body>" +
                            "</html>";

                        webView.loadData(html, "text/html", "UTF-8");
                        onBackPressed();
                        }
                    }

                    return false;
                    }
                });
                }
            }

androidManifest.xml:

                        <?xml version="1.0" encoding="utf-8"?>
                    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                        package="com.journaldev.navigationviewexpandablelistview">

                        <uses-permission android:name="android.permission.INTERNET"/>

                        <application
                        android:allowBackup="true"
                        android:icon="@mipmap/ic_launcher"
                        android:label="@string/app_name"
                        android:roundIcon="@mipmap/ic_launcher_round"
                        android:supportsRtl="true"
                        android:theme="@style/AppTheme">
                        <activity
                            android:name=".MainActivity"
                            android:label="@string/app_name"
                            android:theme="@style/AppTheme.NoActionBar">
                            <intent-filter>
                            <action android:name="android.intent.action.MAIN" />

                            <category android:name="android.intent.category.LAUNCHER" />
                            </intent-filter>
                        </activity>
                        </application>

                    </manifest>

在此处输入代码

0 个答案:

没有答案