一:webview展示详情页面
1. //在adapter中 点击 传值
viewHolder.linear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(context,XQActivity.class);
intent.putExtra("pid",mdata.get(i).getDetailUrl());
context.startActivity(intent);
}
});
//2.在另一个页面接受传值
//布局 只写webview 其他不用
Intent intent=getIntent();
String pid=intent.getStringExtra("pid");
WebSettings settings=webView.getSettings();
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setJavaScriptEnabled(true);
webView.loadUrl(pid);
其他:
1.布局加入webview
2.获取资源id
3.获取数据
webView.loadDataWithBaseURL(null,particularsBean.getResult().getDetails(),"text/html","utf-8",null);
4.
@Override
protected void onDestroy() {
super.onDestroy();
//System.exit(0);
if(webView!=null) {
webView.setVisibility(View.GONE);
webView.removeAllViews();
webView.destroy();
}
}
二:点击输入密码获取输入密码
public class MainActivity extends AppCompatActivity {
private WebView webview;
private String name,pwd;
private SharedPreferences sharedPreferences;
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
webview.setWebViewClient(new WebViewClient());
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
sharedPreferences = getSharedPreferences("AA", Context.MODE_PRIVATE);
name = sharedPreferences.getString("name", "");
pwd = sharedPreferences.getString("pwd", "");
webview.loadUrl("http://ming723.github.io/moni/abnerming.html");
webview.addJavascriptInterface(MainActivity.this, "android");
Log.i("AA", "name===" + name + "pwd====" + pwd);
webview.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webview.loadUrl("javascript:createUser("+name+","+pwd+")");
}
});
}
@JavascriptInterface
public void login(String name,String pwd)
{
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("name",name);
editor.putString("pwd",pwd);
editor.commit();
}
}