我是编程的初学者,我想知道是否有人可以帮助我完成我的代码。我希望执行以下操作:
当用户第一次启动应用程序时:
有人非常友好地指出以下链接似乎提供了解决方案,但是我很难将其实现到我的代码中。 链接:
Load local HTML file into WebView 要么 JavaScript post request like a form submit
感谢所有审核我的代码并帮助我完成代码的人。 也觉得要清理它,因为我确定它是一团糟。 代码:
MainActivity.JAVA
package com.example.removed;
import java.net.URLEncoder;
import org.apache.http.util.EncodingUtils;
import android.net.Uri; import android.os.Bundle; import android.content.DialogInterface; import android.content.Intent;
import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.text.Editable; import android.view.KeyEvent; import android.webkit.CookieManager; import android.webkit.CookieSyncManager; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.EditText;
public class MainActivity extends Activity {
private WebView myWebView;
@SuppressLint("SetJavaScriptEnabled") @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new HelloWebViewClient());
if (savedInstanceState!=null){
myWebView.restoreState(savedInstanceState);
}else{
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("login");
alert.setMessage("Please enter your User Name");
// Set an EditText view to get user input
final EditText inf_username = new EditText(this);
alert.setView(inf_username);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = inf_username.getText().toString();
// Do something with value!
String postData = "username=inf_username";
myWebView.postUrl("removed",EncodingUtils.getBytes(postData, "BASE64"));
myWebView.loadUrl("removed");
myWebView.setWebViewClient(new HelloWebViewClient());
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
//Toast.makeText(getBaseContext(), browser.getUrl(), Toast.LENGTH_LONG).show();
myWebView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle state) {
myWebView.restoreState(state);
super.onRestoreInstanceState(state);
//Toast.makeText(getBaseContext(), browser.getUrl(), Toast.LENGTH_LONG).show();
}
@Override
public void onResume(){
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
//{
//webview.loadUrl(url);
//return true;
//}
// } //public boolean shouldOverrideUrlLoading(WebView view, String url)
{ if (url.startsWith("tel:")) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); }else if(url.startsWith("mailto:")) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); }else if (url.startsWith("comgooglemaps")) { { url = "geo:" +url; } Intent openUrlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(openUrlIntent); }else if(url.startsWith("http:") || url.startsWith("https:")) { webview.loadUrl(url); } return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK)&& myWebView.canGoBack())
{
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}