layout中一页面有按钮,使用android:onClick="timerStartOnClick";一个EditText;一个TextView。
MainActivity中方法:
。。。
TextView tv;
。。。。
public void timerStartOnClick(View view){
View rootView = getLayoutInflater().inflate(R.layout.framelayout_timer,null);
tv=rootView.findViewById(R.id.timeText);//获取显示计时的TextView
EditText et=rootView.findViewById(R.id.textViewTime);//获取输入的分钟数
long l=Integer.parseInt(et.getText().toString());
if(l==0){
l=40;
}
timer=new CountDownTimer(l*1000*60,1000) {
@Override
public void onTick(long millisUntilFinished) {
long l=millisUntilFinished/1000;//剩余的秒数
Toast.makeText(getApplicationContext(),
tv.getText(),Toast.LENGTH_SHORT).show();
tv.setText(l/60+":"+l%60);//改变显示计时的TextView
}
@Override
public void onFinish() {
}
};
timer.start();
}
编译运行没报错,Toast也显示倒数,但是TextView显示没变。
界面使用ViewPager和Fragment实现导航栏