简体   繁体   English

如何使模拟时钟在点击按钮android上不可见和可见

[英]how to make analog clock invisible and visible on click button android

i working in app and want to invisible and visible the analog clock when i click on buttons 我在应用程序中工作,并且想在单击按钮时看不到和看到模拟时钟

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="vertical" >

<AnalogClock
    android:id="@+id/AnalogClock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

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

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

here is the code that i used but its not working 这是我使用的代码,但无法正常工作

      final    AnalogClock clock =(AnalogClock) findViewById(R.id.AnalogClock);
        Button show= (Button) findViewById(R.id.show);
             Button hide= (Button) findViewById(R.id.hide);


          show.setOnClickListener(new View.OnClickListener() {



                @Override
                public void onClick(View v) {

                    clock.setVisibility(View.VISIBLE);
                }

    });
             hide.setOnClickListener(new View.OnClickListener() {



                @Override
                public void onClick(View v) {

                    clock.setVisibility(View.GONE);
                }

    });

i trying to click on buttons but there is no actions so any one know how to make it works please .... 我试图单击按钮,但是没有任何动作,所以任何人都知道如何使它起作用……。

      AnalogClock clock  = (AnalogClock) v; 
      clock.setVisibility(View.GONE);

this is wrong, v is your button, not the clock. 这是错误的,v是您的按钮,而不是时钟。 you need to just do 你只需要做

clock.setVisibility(View.GONE);

EDIT I've tested your code and it works: 编辑我已经测试了您的代码,它的工作原理是:

public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            final AnalogClock clock = (AnalogClock) findViewById(R.id.AnalogClock);
            Button show = (Button) findViewById(R.id.show);
            Button hide = (Button) findViewById(R.id.hide);

            show.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clock.setVisibility(View.VISIBLE);
                }
            });
            hide.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clock.setVisibility(View.INVISIBLE);
                }
            });
        }

    }

And the layout: 以及布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:orientation="vertical" >

    <AnalogClock
        android:id="@+id/AnalogClock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

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

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

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM