Android: How do I display an updating clock in a TextView

前端 未结 6 976
时光说笑
时光说笑 2020-12-08 02:49

have a clock I want to display in a TextView.

I would have thought there was a few nice functions to do this, but I couldn\'t find anything. I ended up implementing

6条回答
  •  情深已故
    2020-12-08 03:27

    You can use TextClock widget provided by android

    
    

    it requires minimum API level 17

    https://developer.android.com/reference/android/widget/TextClock

    if you want the current time in another text view

    you can use the below code snippet

     tClock = (TextClock) findViewById(R.id.textClock1);
            tView = (TextView) findViewById(R.id.textview1);
            btn = (Button)findViewById(R.id.btnGet);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tView.setText("Time: "+tClock.getText());
                }
            });
        }
    

提交回复
热议问题