简单的问题,如何从shouldOverrideUrlLoading内部调用函数/方法? 我正在寻找像Qt的信号和老虎机这样的东西,以便我可以绕过那个OO-Can-Not-See-Your-Parent-Jail。遗憾的是,使用静态不是一个选项,因为我无法再从该静态访问UI。并且设置我想要作为静态访问的UI元素也不会工作,因为Android API会一直在更改这些元素,使静态表示无效。
这就是我现在所拥有的:
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) this.findViewById(R.id.webView);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView wv, String url)
{
//TODO: Call doUrlHandling somehow...
//wv.loadData("URL:" + url, "text/html", "UTF-8");
return true;
}
});
wv.loadUrl("file:///android_asset/startup.html");
}
public void doUrlHandling(String url)
{ }
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:1)
您可以保存活动/上下文的句柄:
public class MainActivity extends Activity
{
private MainActivity mActivity;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mActivity = this; // Save handle here
setContentView(R.layout.activity_main);
.
.
.
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView wv, String url)
{
mActivity.doUrlLoading(url); // call method of MainActivity
return true;
}
});
.
.
.
或通过以下方式引用内部类中的函数:
MainActivity.this.doUrlLoading(url);
有关此语法的详细解释,请参阅Meaning of .this and .class in java