0

我正在为我的 android 创建一个计时器应用程序。它从指定时间(即 8 分钟)倒计时到 0。

我已经对其进行了编码,当应用程序启动时它会显示指定的时间。当您单击TextView显示时间时,它开始倒计时更新TextView事件onTick()。当您再次单击时TextView,它将停止倒计时并保存剩余的毫秒以TextView在下次单击时恢复(基本上暂停时钟)。

一切都按预期工作。但是,当TextView单击它时,它会消失,直到释放单击为止。如果我按住单击 3 秒钟,则在按住时TextView完全消失,并在释放后立即重新出现。

我怎样才能停止这种行为以继续显示TextView整个时间的价值?

编辑:

    tvPeriod.setOnLongClickListener( new View.OnLongClickListener() {           
        @Override
        public boolean onLongClick(View v) {
            valPeriod = 1;
            tvPeriod.setText( tvPeriodPrefix + "1" );

            return true;
        }
    });
4

2 回答 2

0

You can probably do something like set the background to a color, or even transparent. Actually on the lower level, a Button is descended from a TextView, so that may explain why it can act like a Button.

于 2012-12-13T17:56:17.610 回答
0

试试这个示例代码。

import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView tv;
    CountDownTimer countDownTimer;
    boolean isRunning = false;
    long remainingTime = 90000;
    long elapsedTime = 0;

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

        tv = (TextView) findViewById(R.id.tv1);
        tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isRunning) {
                    isRunning = false;
                    countDownTimer.cancel();
                    remainingTime = elapsedTime;
                } else {
                    countDownTimer = new CountDownTimer(remainingTime, 1000) {

                        public void onTick(long millisUntilFinished) {
                            elapsedTime = millisUntilFinished;
                            tv.setText("seconds remaining: "
                                    + millisUntilFinished / 1000);

                        }

                        public void onFinish() {
                            tv.setText("done!");
                        }
                    }.start();
                    isRunning = true;
                }

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
于 2013-01-07T06:15:08.703 回答