【发布时间】:2020-10-31 23:06:27
【问题描述】:
这段代码在我之前正在开发的应用程序上运行,但现在它根本不会做任何事情,这应该是我在使用这段代码之前必须忘记的一件简单的事情,但我不知道它是什么。
我想要完成的是让按钮消失并使布局可见。
这是我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E675E8">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFFFFF"
android:layout_marginBottom="2dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1.0">
<ImageView
android:src="@drawable/natlilandbarpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="@drawable/drawbtn"
android:id="@+id/drawbutton"
android:visibility="visible"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:layout_weight="1.0">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Text"
android:id="@+id/webView"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:gravity="left"
android:layout_marginLeft="100dp"
android:id="@+id/drawlist">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
这是我的活动:
public class MainActivity extends Activity
{
private WebView webView;
private Button drawbutton;
private LinearLayout drawlist;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getActionBar().hide();
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("");
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
}
public void addListenerOnButton() {
drawbutton = (Button) findViewById(R.id.drawbutton);
drawbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View p) {
drawbuttonprop(p);
}
});
drawlist = (LinearLayout) findViewById(R.id.drawlist);
}
public void drawbuttonprop(View view)
{
drawbutton.setVisibility(View.GONE);
drawlist.setVisibility(View.VISIBLE);
}
}
【问题讨论】:
-
您永远不会调用您的
addListenerOnButton()方法,因此永远不会分配drawbutton和drawlist,并且永远不会设置OnClickListener。 -
所以 drawbuttonprop 不起作用?
-
那么分配它们的正确方法是什么?
-
您调用
drawbuttonprop()的唯一位置是您在addListenerOnButton()中设置的OnClickListener。不过,您目前没有打电话给addListenerOnButton(),因此永远不会被设置。我不确定您所说的“正确方式”是什么意思,但如果您只是在onCreate()之后在setContentView()之后调用您的addListenerOnButton(),那么您所拥有的应该可以工作,假设这是正确的当前布局。 -
天哪,我知道我忘记了 tnx 伙计,这让我发疯了