【问题标题】:Android Button ListernersAndroid 按钮监听器
【发布时间】:2013-02-19 21:16:31
【问题描述】:

我对这个 Android 应用程序很陌生,有几个问题涉及 onclick 监听器。

现在我在 ScrollView 中有一个表格,其中一列包含多个按钮。 我的目标是达到一个点,我可以单击一个按钮,然后第二个表格会出现在第一个表格旁边(与第一个表格相同的布局),但有一组不同的按钮。有人可以指导我正确的方向吗?

如果您有任何问题,请告诉我。

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation='vertical' >

 <ScrollView 
 android:layout_width="125dp" 
 android:layout_height="200dp"   >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout android:id="@+id/table_right"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow android:id="@+id/row_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/Button_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button 1"
                android:textSize="20dp" >   
             </Button>
           </TableRow>

         <TableRow android:id="@+id/row_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

                <Button android:id="@+id/Button_2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 2"
                    android:textSize="25dp" > 
                </Button>
            </TableRow>

         <TableRow android:id="@+id/row_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

                <Button android:id="@+id/Button_3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Button 3"
                    android:textSize="25dp" > 
                </Button>
            </TableRow> 

         <TableRow android:id="@+id/row_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

                <Button android:id="@+id/Button_4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Button 4"
                    android:textSize="25dp" > 
                </Button>
            </TableRow>

          <TableRow android:id="@+id/row_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

                <Button android:id="@+id/Button_5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Button 5"
                    android:textSize="25dp" > 
                </Button>
            </TableRow>

          <TableRow android:id="@+id/row_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

                <Button android:id="@+id/Button_6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Button 6"
                    android:textSize="25dp" > 
                </Button>
            </TableRow>

    </TableLayout>  
        </LinearLayout> 
            </ScrollView>
            </LinearLayout>

【问题讨论】:

  • 是的,请列出您的代码以便我们为您提供帮助
  • 为按钮设置标签并处理它。
  • 我添加了到目前为止的内容

标签: android layout button tablelayout onclicklistener


【解决方案1】:

要在可见和不可见之间切换表格,我会建议您使用以下示例:-

            boolean visible = true;
        private void showHide() {

            if (visible) {
            if(table1.getVisibility() == View.Invisible ); { 
                 table1.setVisibility(View.Visible) 
            }   
            }else table1.setVisibility(View.Invisible);

            visible = !visible;
        }

然后在您的button .setOnClickListener 添加showHide(); 现在,当您单击按钮时,它将显示table1,当您再次单击它时,它将隐藏它。希望对你有所帮助。

【讨论】:

  • 这是java ??在android平台下。
【解决方案2】:

在布局 XML 文件中,创建两个表...但将第二个表的可见性设置为“GONE”。 GONE 的可见性意味着它甚至不会占用布局中的任何空间。

我假设您知道如何在按下按钮时执行一些代码(如果不知道,请查看 onclicklistener 或 android:onClick xml 快捷方式)。当按下相应的按钮时,将第二个表格的可见性设置为 VISIBLE,它就会出现。

【讨论】:

  • 所以我可以通过 onclicklisterner 让表格可见并消失
  • 有没有办法让第一张桌子变暗而让第二张桌子突出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-25
  • 2018-08-07
  • 1970-01-01
  • 2013-12-24
  • 2013-07-01
  • 1970-01-01
相关资源
最近更新 更多