首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android中的按钮可见性问题

Android中的按钮可见性问题
EN

Stack Overflow用户
提问于 2012-02-07 11:56:01
回答 3查看 4.3K关注 0票数 1

我正在创建一个工具栏,当您单击工具栏上的按钮时,它将切换按钮的可见性。因此,如果用户单击“绘图”按钮,“铅笔”和“钢笔”的无形按钮将在“绘制”按钮上方可见。如果再次单击“绘制”按钮,则“铅笔”和“钢笔”按钮将再次变得不可见。

在我的xml文件中,我已经将一些按钮的可见性设置为“不可见”,所以当我启动活动时,它们将不会被看到。这部分是笔直的。

.xml btnDrawLine文件- (Update @ 12:21)

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" >

<com.odhranlynch.testSection.UserInterface
    android:id="@+id/UserInterface"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true" />

<Button
    android:id="@+id/btnDraw"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Draw" />

<Button
    android:id="@+id/btnDrawLine"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnDraw"
    android:layout_alignParentLeft="true"
    android:visibility="visible"
    android:text="Line" />

<Button
    android:id="@+id/btnDrawCurve"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnDrawLine"
    android:layout_alignParentLeft="true"
    android:visibility="visible"
    android:text="Curve" />

<Button
    android:id="@+id/btnCutout"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/btnDraw"
    android:text="Cutout" />

<Button
    android:id="@+id/btnCutInner"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnDraw"
    android:layout_toRightOf="@+id/btnDraw"
    android:visibility="visible"
    android:text="Inner" />

<Button
    android:id="@+id/btnCutOutter"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btnDrawCurve"
    android:layout_alignBottom="@+id/btnDrawCurve"
    android:layout_toLeftOf="@+id/btnCancel"
    android:visibility="visible"
    android:text="Outter" />

<Button
    android:id="@+id/btnCancel"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/btnFinish"
    android:text="Cancel" />

<Button
    android:id="@+id/btnFinish"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Finish" />

</RelativeLayout>

接下来,当用户单击一个可见的按钮时,我希望出现不可见的按钮。

事情是这样的,他们不会再出现了!哈哈,我被它弄糊涂了。

如果有人能帮我解释一下这件事,我会非常感激的:)

testActivity.java

代码语言:javascript
运行
复制
package com.odhranlynch.testSection;

import com.odhranlynch.testSection.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class testActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_product);

    // Find buttons and give them a name.
    final View btnDraw = findViewById(R.id.btnDraw);
    final View btnCutOut = findViewById(R.id.btnCutout);
    final View btnDrawLine = findViewById(R.id.btnDrawLine);
    final View btnDrawCurve = findViewById(R.id.btnDrawCurve);
    final View btnCutInner = findViewById(R.id.btnCutInner);
    final View btnCutOutter = findViewById(R.id.btnCutOutter);


    //Draw Button clicked (UI Toolbar).
    btnDraw.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            //Treat button as a toggle button
            //So if a sub-button (e.g. Draw Line) is visible, then we know the button has
            //been toggled to visible so lets now make it invisible.

            if (btnDrawLine.getVisibility()== View.VISIBLE) {
                //Its visible.
                btnDrawLine.setVisibility(View.INVISIBLE);

                btnDrawCurve.setVisibility(View.INVISIBLE);
                Log.d("TAG", "INVISIBLE");
            } else {
                // Either gone or invisible
                btnDrawLine.setVisibility(View.VISIBLE);
                btnDrawCurve.setVisibility(View.VISIBLE);
                Log.d("TAG", "VISIBLE");
            }
        }
    });     

}
}

此外,如果我将按钮的可见性设置为在.xml文件中可见,则可以在运行时切换visibilty。

我再一次感谢你的帮助:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-02-07 12:37:08

尝试将View.INVISIBLE替换为View.GONE

票数 0
EN

Stack Overflow用户

发布于 2012-02-07 12:09:21

你的代码运行良好..。

XML文件

代码语言:javascript
运行
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<Button
android:id="@+id/btnDrawLine"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="@+id/btnDraw"
android:layout_alignParentLeft="true"
android:visibility="invisible"
android:text="Line" />

<Button
    android:id="@+id/draw"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="draw" />

</LinearLayout>

活动

代码语言:javascript
运行
复制
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class DrawCanvasActivity extends Activity {
private static final String Number1 = "9686801147";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final View btnDrawLine = findViewById(R.id.btnDrawLine);


    Button btnDraw = (Button) findViewById(R.id.draw);
    btnDraw.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (btnDrawLine.getVisibility()== View.VISIBLE) {
                btnDrawLine.setVisibility(View.INVISIBLE);
                Log.d("TAG", "INVISIBLE");
            } else {
                btnDrawLine.setVisibility(View.VISIBLE);
                Log.d("TAG", "VISIBLE");
            }
        }
    });     
}
}

当“绘制”按钮为“clicked.Guess”时,将出现按钮行,代码中的视图格式可能会出现问题。

票数 0
EN

Stack Overflow用户

发布于 2012-10-18 15:29:27

如果您不想使用“已消失/可见”,只需在按钮周围添加一个LinearLayout,就可以将其隐藏起来。LinearLayout将有一个layout_width=wrap_content;您可以将其他元素的位置引用到此布局中。

在您自由地将可见性更改为“消失/可见”按钮之后。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9175818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档