【发布时间】:2015-12-13 01:07:40
【问题描述】:
我在 WebView 中的第一个应用程序中。我的应用程序中有一个页面,允许用户从设备的照片库中上传图片以更改头像。
但是,当我测试应用程序时,该按钮不执行任何操作。我点击,点击,点击,双击!什么都没有发生...
我看过很多帖子,我也尝试了很多,似乎没有任何效果,有人说在 webview 中不可能,其他人说它不再是棒棒糖等相同的方法......换句话说:我迷路了!
这是我的 MainActivity.java 的代码:
package com.example.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) 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);
// Use remote resource
mWebView.loadUrl("http://urlofmywebsite");
// Stop local links and redirects from opening in browser instead of WebView
//mWebView.setWebViewClient(new MyAppWebViewClient());
// Use local resource
// mWebView.loadUrl("file:///android_asset/www/index.html");
}
// Prevent the back-button from closing the app
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} 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.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);
}
}
【问题讨论】: