如何使android计时器在点击时不可见和可见

发布于 2024-12-04 07:29:25 字数 2064 浏览 2 评论 0原文

我正在开发一个应用程序,该应用程序的标题栏位于左侧,带有计时器,文本视图位于相对布局的中心。

带有计时器和 TextView 的标题栏

relativeLayout 获取文本视图的高度并填充屏幕宽度

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titlecontainer"
    android:orientation="vertical"
    android:padding="5dip" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" android:background="@color/titlebckgrnd">

我想在用户单击计时器时隐藏计时器并取消隐藏用户再次单击。

如何实现这一目标?

编辑,进一步回答和评论

这是颜色文件代码,

<?xml version="1.0" encoding="utf-8"?>
   <resources>
   <color name="titlebckgrnd">#FFD55A2E</color>
   <color name="titletext">#FFFFFFFF</color>
 </resources>

我按照建议使用了以下代码,

final TextView chron = (TextView)findViewById(R.id.chronometer);
    ((Chronometer) chron).start();
 chron.setOnClickListener(new OnClickListener() {
        private boolean mToggle = true;
        @Override
        public void onClick(View v) {
                Log.d("Gaurav", "Invisible");
                if(mToggle) { 
                    Log.d("Gaurav", String.valueOf(chron.getCurrentTextColor()));
                    chron.setTextColor(R.color.titlebckgrnd);
                    mToggle = false;
                }
                else {
                                          chron.setTextColor(R.color.titletext);
                                          mToogle = true;
                                    } 


                //chronImage.setVisibility(View.VISIBLE);

                //v.setVisibility(View.INVISIBLE);

        }
    });

但结果是

在此处输入图像描述

并且不再响应任何点击。

LogCat 结果

在此处输入图像描述 在此处输入图像描述

即使调试器断点也显示 Textcolor 值发生变化,但显示中的颜色不会发生变化。

I am working on app which has a titlebar with chronometer on the left and a textview centered in a RelativeLayout.

Title bar with Chronometer and TextView

RelativeLayout take height of textview and fill screen width

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titlecontainer"
    android:orientation="vertical"
    android:padding="5dip" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" android:background="@color/titlebckgrnd">

I want to hide the chornometer when user clicks on it and unhide it user clicks again.

How this can be achieved?

EDIT, further to the answers and comments:

Here is the color file code

<?xml version="1.0" encoding="utf-8"?>
   <resources>
   <color name="titlebckgrnd">#FFD55A2E</color>
   <color name="titletext">#FFFFFFFF</color>
 </resources>

I used the following code as suggested

final TextView chron = (TextView)findViewById(R.id.chronometer);
    ((Chronometer) chron).start();
 chron.setOnClickListener(new OnClickListener() {
        private boolean mToggle = true;
        @Override
        public void onClick(View v) {
                Log.d("Gaurav", "Invisible");
                if(mToggle) { 
                    Log.d("Gaurav", String.valueOf(chron.getCurrentTextColor()));
                    chron.setTextColor(R.color.titlebckgrnd);
                    mToggle = false;
                }
                else {
                                          chron.setTextColor(R.color.titletext);
                                          mToogle = true;
                                    } 


                //chronImage.setVisibility(View.VISIBLE);

                //v.setVisibility(View.INVISIBLE);

        }
    });

but the result is

enter image description here

and it does not respond to any more clicks.

LogCat Results

enter image description here
enter image description here

Even the debugger breakpoints show change in Textcolor value but color change in display does not happen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风也等你 2024-12-11 07:29:25

如果我正确理解你的问题,你可以这样做来隐藏计时器:

// We are in your Activity:

final View chronometer= findViewById(R.id.chronometer);

chronometer.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        chronometer.setVisibility(View.GONE); 
        // Use View.INVISIBLE if you would like to keep the room for this view
    }
});

但是将不再显示视图。因此,根据用户应单击的位置以使其再次显示,您可能必须为视图和/或其文本提供与背景相同的颜色,而不是使它们不可见:

// should chronometer be a TextView that displays the time:

final TextView chronometer = (TextView) findViewById(R.id.chronometer);

chronometer.setOnClickListener(new View.OnClickListener() {
    private boolean mToggle = true;

    public void onClick(View v) {
        if (mToggle ) {
            chronometer.setTextColor(Color.BLACK); 
            mToggle = false;
        }
        else {
            chronometer.setTextColor(Color.WHITE); 
            mToggle = true;
        }
    }
});

If I properly understood your question you can do that way to hide the chronometer:

// We are in your Activity:

final View chronometer= findViewById(R.id.chronometer);

chronometer.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        chronometer.setVisibility(View.GONE); 
        // Use View.INVISIBLE if you would like to keep the room for this view
    }
});

But there will be no view displayed anymore. So depending of where the user should click to have it displayed again, you may have to give your view and/or its text the same color as the background instead of making them invisible:

// should chronometer be a TextView that displays the time:

final TextView chronometer = (TextView) findViewById(R.id.chronometer);

chronometer.setOnClickListener(new View.OnClickListener() {
    private boolean mToggle = true;

    public void onClick(View v) {
        if (mToggle ) {
            chronometer.setTextColor(Color.BLACK); 
            mToggle = false;
        }
        else {
            chronometer.setTextColor(Color.WHITE); 
            mToggle = true;
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文