Android实现简单的倒计时功能
public class MainActivity extends Activity { private TextView textview; private Button but1; private int count=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview=(TextView)findViewById(R.id.mytextview); but1=(Button)findViewById(R.id.but1); But1 b=new But1(); but1.setOnClickListener(b); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } class But1 implements OnClickListener{ @Override public void onClick(View v) { new CountDownTimer(30000,1000) { @Override public void onTick(long millisUntilFinished) { textview.setText("请等待30秒(" + millisUntilFinished / 1000 + ")..."); } @Override public void onFinish() { Toast.makeText(getApplicationContext(), "倒计时结束", Toast.LENGTH_SHORT).show(); } }.start(); } } }
本文出自 “Java大白的战地” 博客,请务必保留此出处http://8023java.blog.51cto.com/10117207/1657588
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。