1、使用Timer定时器定时和取消定时
需要结合使用Timer和TimeTask两个类来完成
其中TimeTask的类对象作为Timer对象的schedule方法的参数
timer.scheule(timerTask,delay,period)//任务,延迟,间隔
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Timer timer=null;
private int start=0;
private TextView showTxt=null;
private Button btnStopTime,btnStartTime;
private EditText ededit;
private String[] showInfos={"北京大学","清华大学","北京航空航天大学"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化组件
init();
updateTime();
btnStopTime.setOnClickListener(this);
btnStartTime.setOnClickListener(this);
changeInfo();
}
//事实接受edit编辑框内容改变show文本框内容
private void changeInfo() {
TimerTask timerTask=new TimerTask() {
@Override
public void run() {
String string =ededit.getText().toString();
showTxt.setText(string);
}
};
timer.schedule(timerTask,0,10);
}
private void updateTime() {
timer=new Timer();
TimerTask timerTask=new TimerTask() {
@Override
public void run() {
//Log.d("TAG", "run: "+adding());
showTxt.setText(adding()+"");
}
};
timer.schedule(timerTask,0,1000);
}
public void init(){
showTxt=findViewById(R.id.showHi);
btnStopTime=findViewById(R.id.btnStopTime);
btnStartTime=findViewById(R.id.btnSartTime);
ededit=findViewById(R.id.ededit);
}
public void showInfo(String [] strings){
String show="";
for (String str :strings) {
show+=str+"\n";
}
showTxt.setText(show);
}
private int adding(){
start+=1;
return start;
}
@Override
public void onClick(View v) {
Log.d("TAG", "onClick: ");
switch (v.getId()){
case R.id.btnStopTime:
timer.cancel();;
break;
case R.id.btnSartTime:
updateTime();
break;
}
}
}
2、使用CountDownTimer类做一个输入提示(实际是倒计时)
- 使用了自定义的MyCountDonwTimer 继承自CountDownTimer
- 创建了一个自定义类MyCountDonwTimer的对象,在构造方法中约定了延迟时间和持续时间
- 通过start()方法和cancel方法开启和结束
- CountDownTimer类需要时间的方法中ontick()和onfinsh()方法的使用
public class MainActivity extends AppCompatActivity {
private TextView hints;
private EditText edinput;
private MyCountDonwTimer mMyCountDonwTimer = new MyCountDonwTimer(5000, 1000);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
edinput.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
hints.setText("text is inputing");
hints.setTextColor(Color.RED);
}
@Override
public void afterTextChanged(Editable s) {
mMyCountDonwTimer.cancel();
mMyCountDonwTimer.start();
}
});
}
private void initView() {
hints=findViewById(R.id.hints);
edinput=findViewById(R.id.edinput);
}
class MyCountDonwTimer extends CountDownTimer{
public MyCountDonwTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
hints.setText("");
}
}
}
3、使用CountDownTimer类做倒计时
public class MainActivity extends AppCompatActivity {
private Button btnGet;
private CountTimer mCountTimer=new CountTimer(10*1000, 1000);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGet=findViewById(R.id.btnGet);
btnGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCountTimer.start();
}
});
}
class CountTimer extends CountDownTimer{
public CountTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
btnGet.setEnabled(false);
btnGet.setText(millisUntilFinished/1000+"秒");
}
@Override
public void onFinish() {
btnGet.setEnabled(true);
btnGet.setText("重新获取");
}
}
}
4、模拟耗时操作,子线程更新主线程界面
- 一定要在子线程中更新主线程的ui,所以要开启一个新的线程new thread(){}.start()
- 模拟耗时操作使用Thread.sleep(时间)操作
- 进度条的进度使用setProgress(数值)
public class MainActivity extends AppCompatActivity {
private Button btnStart;
private ProgressBar mProgressBar;
private int max=100;
private int current=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(){
@Override
public void run() {
while (current<=max){
try {
Thread.sleep(50);
current++;
mProgressBar.setProgress(current);
} catch (Exception e) {
e.printStackTrace();
}
}
current=0;
}
}.start();
}
});
}
private void initView() {
btnStart=findViewById(R.id.button);
mProgressBar=findViewById(R.id.progressBar);
}
}
5、弹出对话框
- 重点在语要建立Dialog dialog=new AlertDialog.Builder()创建一个AlertDailog对象,并在此基础上设定标题,图片,按钮等
- dialog一定要以.create()结束完成创建
- dialog一定要以dialog.show()方法显示出来
- setPostiveButton和negativeButton一定要传一个事件,通常是点击事件,业务逻辑在事件中写
- setSingleChoiceItems和setMultiChoiceItems设置单选按钮和多选按钮对话框
- StringBuilder一定要new一个出来再使用
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mButton1,mButton2,mButton3;
private String[] singleChoice={"java","python","swift"};
private String[] multiChoice={"股票","基金","债券"};
private String choice="";
private StringBuilder multiString=new StringBuilder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
//初始化控件
private void initView() {
mButton1=findViewById(R.id.button1);
mButton2=findViewById(R.id.button2);
mButton3=findViewById(R.id.button3);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
}
//按钮事件
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
Log.d("TAG", "onClick: 按钮1");
alertDialog() ;
break;
case R.id.button2:
singleDialog();
break;
case R.id.button3:
multiDialog();
break;
default:
break;
}
}
//弹出多选对话框
private void multiDialog() {
Dialog dialog=new AlertDialog.Builder(this)
.setIcon(R.drawable.arrowss)
.setTitle("选择一个你最频繁使用的语言")
.setMultiChoiceItems(multiChoice, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked){
multiString.append(multiChoice[which]+"--");
}
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), multiString, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
dialog.show();
}
//弹出单选对话框
private void singleDialog() {
Dialog dialog=new AlertDialog.Builder(this)
.setIcon(R.drawable.arrowss)
.setTitle("选择一个你最频繁使用的语言")
.setSingleChoiceItems(singleChoice, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
choice=singleChoice[which];
Log.d("TAG", "onClick: "+singleChoice[which]);
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), choice+"被选中", Toast.LENGTH_SHORT).show();
}
})
.create();
dialog.show();
}
//弹出对话框
private void alertDialog() {
Dialog dialog= new AlertDialog.Builder(this)
.setIcon(R.drawable.arrowss)
.setTitle("请记住这是一个对话框")
.setMessage("你做了一项惊天动地的事情")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("TAG", "onClick: 你点击了确定按钮");
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("TAG", "onClick: 你点击了取消按钮");
}
})
.create();
dialog.show();
}
}
6、弹出进度条对话框
通过 final ProgressDialog progressDialog=new ProgressDialog(this);来创建进度条对话框
使用incrementProgressBy增加对话框
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnStart=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart=findViewById(R.id.button);
btnStart.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.button){
startProgress();
}
}
private void startProgress() {
final ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.setTitle("正在请求网络");
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(0);
progressDialog.setButton("隐藏", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.dismiss();
}
});
progressDialog.onStart();
progressDialog.show();
new Thread(){
@Override
public void run() {
try {
int i=0;
while (i<100){
Thread.sleep(50);
progressDialog.incrementProgressBy(1);
//progressDialog.setProgress(i);//使用setProgress也是可以的
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}