有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用导航抽屉中的字符串链接本地HTML页面?

我有一个带有webview的简单应用程序,可以加载本地HTML页面,我将所有文件放在资产中,现在在我的导航抽屉中,我希望有一些文本/链接可以打开这些页面,我尝试在web上跟踪一些Tut,但不知怎的,我无法做到这一点

任何帮助都将不胜感激

我在安卓 studio上的代码:

AndroidManifest。xml

<?xml version="1.0" encoding="utf-8"?>

<application
    安卓:allowBackup="true"
    安卓:icon="@drawable/ic_launcher"
    安卓:label="@string/app_name"
    安卓:theme="@style/AppTheme" >
    <activity
        安卓:name=".MainActivity"
        安卓:label="@string/app_name" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />

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

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

主要活动。xml

<安卓.support.v4.widget.DrawerLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/drawer_layout"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">


<RelativeLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:paddingLeft="@dimen/activity_horizontal_margin"
    安卓:paddingRight="@dimen/activity_horizontal_margin"
    安卓:paddingTop="@dimen/activity_vertical_margin"
    安卓:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    安卓:background="#ffffffff">

    <TextView
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_centerHorizontal="true"
        安卓:text="History Of S.Johnson High School"
        安卓:textSize="24sp"
        安卓:gravity="center"
        安卓:layout_marginTop="100dp"/>

    <ImageView
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:src="@drawable/sjohnson"
        安卓:layout_alignParentBottom="true"
        安卓:layout_centerHorizontal="true"/>

    <WebView
        安卓:id="@+id/activity_main_webview"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent" />

</RelativeLayout>

<!-- Side navigation drawer UI -->
<ListView
    安卓:id="@+id/navList"
    安卓:layout_width="250dp"
    安卓:layout_height="match_parent"
    安卓:layout_gravity="left|start"
    安卓:background="#ffeeeeee"/>

主要活动。java

public class MainActivity extends ActionBarActivity {
private WebView mWebView;
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;

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

    mWebView = (WebView) findViewById(R.id.activity_main_webview);

    mDrawerList = (ListView)findViewById(R.id.navList);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();

    addDrawerItems();
    setupDrawer();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new MyAppWebViewClient());
    mWebView.loadUrl("file:///安卓_asset/www/index.html");


}

@Override
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

private void addDrawerItems() {
    String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
    mAdapter = new ArrayAdapter<String>(this, 安卓.R.layout.simple_list_item_1, osArray);
    mDrawerList.setAdapter(mAdapter);

    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
        }
    });
}

private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {


        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getSupportActionBar().setTitle("Navigation!");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }


        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle(mActivityTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@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_settings) {
        return true;
    }

    // Activate the navigation drawer toggle
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

MyAppWebViewClient。java

public class MyAppWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(Uri.parse(url).getHost().length() == 0) {
        return false;
    }

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    view.getContext().startActivity(intent);
    return true;
}

主题。xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme"
    parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="安卓:actionBarStyle">@style/MyActionBar</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
    parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="安卓:background">@drawable/actionbar_background</item>

    <!-- Support library compatibility -->
    <item name="background">@drawable/actionbar_background</item>
</style>

字符串。xml

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">Info</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>


共 (1) 个答案

  1. # 1 楼答案

    据我所知,您想通过单击抽屉菜单中的项目将您的html文件从资产加载到WebView?如果是这样,您需要将以下行添加到抽屉中listView的onClickListener中:

    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
            //load file to webView
            mWebView.loadUrl("REPLACE_IT_WITH_PATH_TO_FILE");
            //close drawer
            mDrawerLayout.closeDrawers();
        }
    });