进度对话框(ProgressDialog)

在进度对话框中,主要是以主函数为主,加入线程。

其中string.xml的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">ProgressDialog</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="ok">确定</string>
    <string name="title">安装进度</string>


</resources>

main.xml的代码就是加一个Button按键,这里就不写出来了,

对于可视为重中之重的主函数,这里会做详细的解释:

package com.example.progressdialog;


import android.R.integer;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;




public class MainActivity extends Activity {

final int PROGRESS_DIALOG=0;//声明进度对话框id。
final int INCREASE=0;//Handler消息类型。
ProgressDialog progressDialog;//进度对话框的引用。
Handler myHandler;//Handler对象的引用。


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)this.findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
showDialog(PROGRESS_DIALOG);//显示进度对话框。

}
});
        myHandler=new Handler(){ //创建Handler对象。
        public void handleMessage(Message msg){
        switch (msg.what) {
case INCREASE:
progressDialog.incrementProgressBy(1);//进度每次加1。
if(progressDialog.getProgress()>=100){//判断是否结束进度。
progressDialog.dismiss();//如果进度条走完则关闭窗口。
}

break;


default:
break;
}
        super.handleMessage(msg);
        }
        };
        
    }
    public Dialog onCreateDialog(int id){
    //Dialog dialog=null;
    switch (id) {
case PROGRESS_DIALOG://对id进行判断。
progressDialog=new ProgressDialog(this);//创建进度对话框。
progressDialog.setMax(100);//设置最大值
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle(R.string.title);//设置标题。
progressDialog.setCancelable(false);//设置进度对话框不可以用回退按钮关闭。

break;


default:
break;
}
    return progressDialog;
    }
    public void onPrepareDialog(int id,Dialog dialog){//每次弹出对话框时被回调以动态更新对话框内容。
    super.onPrepareDialog(id, dialog);
    switch (id) {
case PROGRESS_DIALOG:
progressDialog.incrementProgressBy(-progressDialog.getProgress());//对话框进度清零
new Thread(){ //创建一个线程。
public void run(){
while(true){
myHandler.sendEmptyMessage(INCREASE);//发送Handler消息。
if(progressDialog.getProgress()>=100){
break;
}
try{Thread.sleep(40);}//线程休眠。
catch (Exception e) {
e.printStackTrace();
}
}
}
}.start(); //启动线程。

break;


default:
break;
}
    }


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


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这里面基本的难点都表现出来了,

而关于线程的知识,我会在下面的课题中写到,(只是现在我也没有了解到)希望每天都有一些收获吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FileSystemView fileSystemView=FileSystemView.getFileSystemView(); File tempFile; if (filepath==null||filepath.isEmpty()||filepath.equals("")) { tempFile=fileSystemView.getHomeDirectory(); }else { tempFile=new File(filepath); } JFileChooser chooser = new JFileChooser(tempFile); //设置选择器 chooser.setMultiSelectionEnabled(true); //设为多选 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置可选择的为文件和文件夹 chooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter() { @Override public String getDescription() { // TODO Auto-generated method stub return ".xml"; } @Override public boolean accept(File f) { // TODO Auto-generated method stub if (f.getName().toLowerCase().endsWith(".xml")) { return true; } return false; } }); chooser.setDialogTitle("请选择需要修改的文件"); chooser.setPreferredSize(new Dimension(800,1000)); chooser.getActionMap().get("viewTypeDetails").actionPerformed(null); chooser.showOpenDialog(button); //显示打开文件选择框 filepath = chooser.getSelectedFile().getAbsolutePath(); //获取绝对路径 button.setText(filepath); if (chooser.isFileSelectionEnabled()) { new MyJOptionPane("目前仅支持修改xml格式文件,请选择正确的格式"); } if(chooser.isDirectorySelectionEnabled()) { progressBar.setIndeterminate(true); progressBar.setStringPainted(false); progressDialog.setVisible(true); Thread thread=new Thread(() -> { try { XmlHanding.getMyFlie(new File(filepath)); text.setText("已选择文件夹:"+filepath); text.append("\n"+"文件包含"+XmlHanding.fList.size()+"个xml文件"); for(File f:XmlHanding.fList){ text1.append("\n"+f.getAbsolutePath()); } text1.setCaretPosition(0); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); }finally { // 执行完毕后关闭进度对话框 progressDialog.dispose(); } }); thread.start(); } }为什么选择了单个的文件 text还是会写入"已选择文件夹:"+filepath
最新发布
07-15

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值