Java 在我单击按钮创建PDF之前会出现进度条

Java 在我单击按钮创建PDF之前会出现进度条,java,android,pdf,progress-bar,Java,Android,Pdf,Progress Bar,为什么在我点击按钮创建PDF之前会出现进度条? 我希望在创建PDF文件期间显示进度条 这是我的密码: public class TwoFragment extends android.support.v4.app.Fragment { private View v; Intent chooser=null; String myInt=""; String ciao=""; private String str

为什么在我点击按钮创建PDF之前会出现进度条? 我希望在创建PDF文件期间显示进度条

这是我的密码:

   public class TwoFragment extends android.support.v4.app.Fragment {

    private View v;
    Intent chooser=null;
    String myInt="";
    String ciao="";
    private String string="";
    private ProgressBar pdfProgress;

    public TwoFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View rootView = inflater.inflate(R.layout.fragment_two, container, false);



        Button mButton = (Button) rootView.findViewById(R.id.newbutton);
        mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                //sendemail();
                pdfProgress.setVisibility(View.VISIBLE);
                createPDF();
                pdfProgress.setVisibility(View.GONE);
                viewPDF();



            }
        });
        TextView titolo3 = (TextView)rootView.findViewById(R.id.result);
        TextView titolo2 = (TextView)rootView.findViewById(R.id.result2);
        TextView titolo4 = (TextView)rootView.findViewById(R.id.resultpizze);
        pdfProgress = (ProgressBar)rootView.findViewById(R.id.progressbar);
        //pdfProgress.setVisibility(View.GONE);


        //TextView titolo = (TextView)rootView.findViewById(R.id.quantità3);


  Bundle bundle2=getArguments();
        if(bundle2 != null){
             string = bundle2.getString("scelta2");
            titolo3.setText(string);

        }

    /*    Bundle bundle2=getArguments();
        if(bundle2 != null){
           //  myInt = bundle2.getString("scelta2",myInt);
            cacca=bundle2.getString("result",null);

            //cacca=myInt;
           // Log.d("ciao",cacca);
           titolo3.setText(cacca);
        }*/

        //titolo3.setText(myInt);


    /*    Bundle bundle3=getArguments();
        if(bundle3 != null){
           // String myInt3 = bundle3.getString("totalebirre", null);
           //  cazzo2=Integer.parseInt(myInt3);
           int cazzo2=bundle3.getInt("totalebirre");
            titolo2.setText(String.valueOf(cazzo2));



        }
        Bundle bundle=getArguments();
        if(bundle != null){
            // String myInt2 = bundle2.getString("totalepizze", null);
            //   cazzo=Integer.parseInt(myInt2);
            //titolo2.setText(myInt2);
            String string=bundle.getString("scelta3", null);
             titolo4.setText(string);

        }
*/


        return rootView;
    }


/* public void sendemail(){

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setData(Uri.parse("mailto:"));
        String[] to={"marco_marcoletto@hotmail.it"};
        intent.putExtra(Intent.EXTRA_EMAIL,to);
        intent.putExtra(Intent.EXTRA_SUBJECT, "ciao");
        intent.putExtra(Intent.EXTRA_TEXT, "zao");
        intent.setType("message/rfc822");
        chooser=intent.createChooser(intent,"manda email");
        startActivity(chooser);
    }*/

   //@TargetApi(Build.VERSION_CODES.M)
    public void createPDF() {

       Document doc = new Document();

       try {
           String path = Environment.getExternalStorageDirectory()
                   .getAbsolutePath() + "/droidText";

           File dir = new File(path);
           if (!dir.exists())
               dir.mkdirs();

           Log.d("PDFCreator", "PDF Path: " + path);

           //File file = new File(dir, "sample.pdf");
           File file = new File(dir, "salve.pdf");

           FileOutputStream fOut = new FileOutputStream(file);

           PdfWriter.getInstance(doc, fOut);

           // open the document
           doc.open();
           ByteArrayOutputStream stream = new ByteArrayOutputStream();
           Bitmap bitmap = BitmapFactory.decodeResource(getContext()
                   .getResources(), R.drawable.androtuto);
           bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
           Image myImg = Image.getInstance(stream.toByteArray());
           myImg.setAlignment(Image.MIDDLE);

           // add image to document
           doc.add(myImg);
           Paragraph p1 = new Paragraph(string);
           Font paraFont = new Font(Font.COURIER);
           p1.setAlignment(Paragraph.ALIGN_CENTER);
           p1.setFont(paraFont);




           // add paragraph to document
           doc.add(p1);

           Paragraph p2 = new Paragraph("Bonjour Android Tuto");

           Font paraFont2 = new Font(Font.COURIER, 14.0f, Color.GREEN);
           p2.setAlignment(Paragraph.ALIGN_CENTER);
           p2.setFont(paraFont2);

           doc.add(p2);


           stream = new ByteArrayOutputStream();
           bitmap = BitmapFactory.decodeResource(getContext()
                   .getResources(), R.drawable.android);
           bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
           myImg = Image.getInstance(stream.toByteArray());
           myImg.setAlignment(Image.MIDDLE);

           // add image to document
           doc.add(myImg);

           // set footer
           Phrase footerText = new Phrase("Pied de page ");
           HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
           doc.setFooter(pdfFooter);

       } catch (DocumentException de) {
          // Log.e("PDFCreator", "DocumentException:" + de);
           Log.e("PDFCreator", "DocumentException:" + de.getMessage());
       } catch (IOException e) {
         //  Log.e("PDFCreator", "ioException:" + e);
           Log.e("PDFCreator", "DocumentException:" + e.getMessage());


       } finally {
           doc.close();
       }

   }
    public void viewPDF(){
        String path = "/sdcard/droidText/salve.pdf";

        File targetFile = new File(path);
        Uri targetUri = Uri.fromFile(targetFile);

        Intent intent;
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(targetUri, "application/pdf");

        startActivity(intent);
    }

}
XML:



fragment\u two
xml中为您的progressbar设置
android:visibility=“Goe”
,只需将
android:visibility=“Goe”
添加到您的
progressbar

<ProgressBar
    android:id="@+id/progressbar"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:layout_centerInParent="true" />

快乐编码。

步骤1-声明全局变量-私有ProgressBar pdfProgress

步骤2-在各自的layour.xml(fragment_two)中创建一个进度条小部件,并给它一个id

步骤3-在TwoFragment的onCreate()方法中,在定义rootView之后编写此代码。pdfProgress=(ProgressBar)rootView.findviewbyd(R.id.“您的进度条id”)

步骤4-就在createPDF()上方;写下-pdfProgress.setVisibility(View.VISIBLE)

步骤5-就在createPDF()下面;写下-pdfProgress.setVisibility(View.GONE)

**

最新答案

**

在TwoFragment-onClick()中尝试此代码-

fragment_two.xml(您的片段布局xml)必须包含一个如下所示的进度条-(您也可以添加可绘图项,可能需要看一个教程)



这是因为默认情况下,
ProgressBar
是可见的。首先使其不可见。我必须在哪里使进度条不可见?在问题中添加
fragment\u two
文件代码我解决了问题。您的progressbar正在显示,但您的
createPDF()
func正在主线程中运行,因此它阻止GUI更新,因此进度不可见。当
createPDF()
完成时,将隐藏进度。您可以通过注释out line
pdfProgress.setVisibility(View.GONE)轻松测试这一点。你会看到进展实际上变得显而易见。唯一的解决方法是在另一个线程中调用
createPDF()
。那么我如何解决这个问题呢?不起作用…现在当我单击按钮创建PDF时进度条不出现…不起作用…现在当我单击按钮创建xml和java文件的PDFpost完整代码时进度条不出现。如果我把pdfProgress.setVisibility(View.GONE)放进去,当前的代码看起来很好;在点击前,应用程序崩溃确保你正在调用点击正确的资源ID
pdfProgress.setVisibility(View.GONE);
Button mButton = (Button) rootView.findViewById(R.id.newbutton);
pdfProgress.setVisibility(View.GONE);
ProgressBar pdfProgress = (ProgressBar)         
        mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                //sendemail();
                pdfProgress.setVisibility(View.VISIBLE);
                createPDF();
                pdfProgress.setVisibility(View.GONE);
                viewPDF();



            }
        });
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/progressBar"/>