簡體   English   中英

在單擊按鈕創建 PDF 之前出現進度條

[英]Progress bar appears before I click on button to create PDF

為什么在單擊按鈕創建 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:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
    <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"
         />
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_centerHorizontal="true" >

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
<!--<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">-->
   <!-- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:weightSum="1">
        <TextView
            android:id="@+id/result2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:capitalize="characters"
            android:layout_centerVertical="false"
            android:layout_marginTop="30dp"
            android:textSize="30dp"/>

        <TextView
            android:id="@+id/quantità2"
            android:layout_width="100dp"
            android:layout_height="500dp"
            android:layout_centerVertical="false"
            android:textSize="30dp"
            android:layout_marginTop="30dp" />
    </LinearLayout>-->




       <LinearLayout
            android:id="@+id/content_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           android:orientation="vertical">

            <TextView
                android:id="@+id/result"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerVertical="false"
                android:textSize="30dp"
                android:layout_marginTop="0dp"/>

           <TextView
               android:id="@+id/resultpizze"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:textSize="30sp"/>

       </LinearLayout>
        <LinearLayout
            android:id="@+id/content_main3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!--<TextView
                android:id="@+id/resultpizze"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerVertical="false"
                android:layout_marginTop="30dp"
                android:textSize="30dp"/>-->


        </LinearLayout>
    <LinearLayout
        android:id="@+id/content_main2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

            <TextView
                android:id="@+id/result2"
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="30sp"
                 />

       <!-- <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Manda Email"
            android:id="@+id/newbutton"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="300dp" />
-->
       </LinearLayout>


<!--</ScrollView>-->
        <Button
            android:id="@+id/newbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_gravity="center_horizontal"
            android:onClick="sendemail"
            android:text="Manda Email">
        </Button>

    </LinearLayout>


    </ScrollView>



</RelativeLayout>

fragment_two xml 中為您的進度條設置android:visibility="gone"

只需將android:visibility="gone"到 XML 中的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" />

您也可以在 Java 中執行此操作。 setContentView之后寫這個

pdfProgress.setVisibility(View.GONE);

快樂編碼。

第一步——聲明一個全局變量——private ProgressBar pdfProgress;

第 2 步 - 在您各自的 layer.xml(fragment_two) 中創建一個進度條小部件並為其指定一個 id。

第 3 步 - 在 TwoFragment 的 onCreate() 方法中,在定義 rootView 之后編寫此代碼。 pdfProgress = (ProgressBar)rootView.findViewById(R.id."你的進度條id");

第 4 步 - 就在 createPDF() 上方; 寫這個 - pdfProgress.setVisibility(View.VISIBLE);

第 5 步 - 就在 createPDF() 下方; 寫這個 - pdfProgress.setVisibility(View.GONE);

**

更新答案

**

在你的 TwoFragment 中試試這個代碼 - onClick()-

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();



            }
        });

fragment_two.xml(你的片段布局 xml)必須包含一個看起來像這樣的進度條 - (你也可以添加可繪制對象,可能想查看教程)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM