Java 项目可见性在脚本中不起作用
我的布局文件中包含以下内容:Java 项目可见性在脚本中不起作用,java,android,Java,Android,我的布局文件中包含以下内容: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" androi
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="@+id/topNav"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stop_surfing"/>
<TextView
style="@style/Counter"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</LinearLayout>
这是第二节课:
public class JavaScriptBinder{
Activity context;
JavaScriptBinder(Activity context){
this.context = context;
}
public void surf(String memberId){
// Snipped code //
LinearLayout top = (LinearLayout)context.findViewById(R.id.topNav);
top.setVisibility(View.VISIBLE);
}
}
surf()
是从webview中加载的javascript文件调用的:
function startSurfing(){
var users = document.getElementsByClassName("user");
for(var i = 0; i < users.length; i++){
users[i].addEventListener("click", function(e){
e.stopPropagation();
with(document.getElementById("black-overlay").style){
display = "block";
backgroundColor = "rgba(0,0,0,0.7)";
}
var userId = this.dataset.id;
$js.surf(userId);
}, false);
}
}
函数startSurfing(){
var users=document.getElementsByClassName(“用户”);
对于(var i=0;i
文档说明代码是在不同的线程中运行的,因此当然,您不能像这样通过此方法更新UI,请参见此处:
- JavaScript在这个WebView的私有后台线程上与Java对象交互。因此,需要注意保持螺纹安全
@JavascriptInterface
注释您的方法,以便该方法可在Android 4.2及更高版本中调用
试试这个:
@JavascriptInterface
public void surf(String memberId){
// Snipped code //
final LinearLayout top = (LinearLayout)context.findViewById(R.id.topNav);
context.runOnUiThread(new Runnable(){
@Override
public void run(){
top.setVisibility(View.VISIBLE);
}
});
}
你不需要做其他的事情。您确定您拥有正确的活动实例,并且您从UI线程调用了该代码吗?我已经更新了question@RyanNaddy你什么时候调用了surf?当点击页面上的一个项目时,我从webview javascript文件调用它
$js.surf(userId)代码>我不确定这是否会解决任何问题,但除了想知道在哪里调用surf之外,我还想移动LinearLayout top=(LinearLayout)context.findViewById(R.id.topNav)将>编码到您的onCreate()
中。您也可以考虑在代码<> XML 中临时生成“<代码>线性布局< /代码>”,以确保您能真正看到它。另一方面,我不得不进入决赛。2) 您在方法runOnUiThread()
上缺少一个)
,但在其他方面它可以工作,谢谢!好的,我添加了)代码>:我很高兴能帮上忙!
@JavascriptInterface
public void surf(String memberId){
// Snipped code //
final LinearLayout top = (LinearLayout)context.findViewById(R.id.topNav);
context.runOnUiThread(new Runnable(){
@Override
public void run(){
top.setVisibility(View.VISIBLE);
}
});
}