2017-02-22 25 views
0

我有一個editText和我有一個按鈕,改變顏色,大小和字體,所以當將文件保存爲pdf或txt文件時如何才能得到這3件事與文本我只得到正常的黑色文本這是我的代碼風格。如何保存editText的顏色,大小,字體?

size.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      txtSpeechInput.setTextSize(25); 
      txtSpeechInput.setTextColor(Color.rgb(150,0,0)); 
      txtSpeechInput.setTypeface(null, Typeface.BOLD); 

保存代碼:

final File externalStorageDir = new File(
        Environment 
          .getExternalStorageDirectory(), "MyVoiceText"); 

      boolean success = true; 
      if (!externalStorageDir.exists()) { 
       success = externalStorageDir.mkdirs(); 
      } 

      if (success) { 
       final Dialog dialog2 = new Dialog(MainActivity.this); 
       dialog2.setContentView(R.layout.dialog_save); 
       dialog2.show(); 
       editsave = (EditText) dialog2.findViewById(R.id.edit_save); 
       pdf = (Button) dialog2.findViewById(R.id.pdf_save); 
       pdf.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         pdf.setEnabled(false); 
         txt.setEnabled(true); 

        } 
       }); 

       txt = (Button) dialog2.findViewById(R.id.txt_save); 
       txt.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         txt.setEnabled(false); 
         pdf.setEnabled(true); 

        } 
       }); 
       dia_save = (Button) dialog2.findViewById(R.id.dia_save); 
       dia_save.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         String savename = editsave.getText().toString(); 

         if(!txt.isEnabled()) { 
          File myFile = new File(externalStorageDir + "/" + savename + ".txt"); 

           File myFile = new File(externalStorageDir + "/" + savename + ".txt"); 

          try { 
           StringBuffer string = new StringBuffer(); 

           string.append(txtSpeechInput.getText().toString()); 
           Properties properties = new Properties(); 
           // set the properties value 
           properties.put("text",string.append(txtSpeechInput.getText().toString()));         properties.put("textstyle","bold"); 
           properties.put("typeface",txtstyle); 
           properties.put("etxtColor",txtcolor); 

           FileOutputStream fOut = new FileOutputStream(myFile); 
           OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
           myOutWriter.append(string); 

           myOutWriter.close(); 
           fOut.flush(); 
           fOut.close(); 
          } catch (Exception e) { 

          } 


          try { 
           myFile.createNewFile(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
          dialog2.dismiss(); 
         }else if(!pdf.isEnabled()){ 
          try 
          { 

           String savename2 = editsave.getText().toString(); 

           String takeit = txtSpeechInput.getText().toString(); 

            Document document = new Document(PageSize.A4); 
           Paragraph p = new Paragraph(); 
           PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory() + "/MyVoiceText/" +savename2+".pdf")); 
           document.open(); 
           writer.getDirectContent(); 
           p.add(new Phrase(takeit)); 



           document.add(p); 

           document.close(); 
           Log.d("OK", "done"); 

         } catch (Exception e) { 
          //LOGGER.error(e.getMessage()); 
         } 

重新加載文本文件

File sdcard = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/MyVoiceText/"); 
         //Get the text file 
        File file = new File(sdcard, String.valueOf(selected)); 
         InputStream input = null; 
         try { 
          input = new FileInputStream(file); 
          Properties prop = new Properties(); 
          // load a properties file 
          prop.load(input); 
         }catch (Exception e){ 
          e.printStackTrace(); 
         } 
         //Read text from file 
         StringBuilder text = new StringBuilder(); 

         try { 
          BufferedReader br = new BufferedReader(new FileReader(file)); 
          String line; 

          while ((line = br.readLine()) != null) { 
           text.append(line); 

          } 
          br.close(); 
         catch (IOException e) { 
          //You'll need to add proper error handling here 
         } 
+0

改變的TextView的大小,字體和顏色是不同的事情,改變PDF文本的大小,字體和顏色。 – nnn

+0

好吧,這是我的問題如何保存文件的自定義顏色,...,.. – Eazyz

+0

爲此,你可以保持你的文字顏色,文字,文字風格,字體的所有portieres到像TextPojo一類,並保持它在Server或本地數據庫或應用程序上下文中。 –

回答

0

通過你的代碼,就好像你正在使用iTextPDF庫生成PDF。 使用字體類iTextPDF創造一個新的字體,並使用它象下面這樣:

private static Font HelveticaBlue = new Font(Font.FontFamily.HELVETICA, 6, 
      Font.BOLD, BaseColor.BLUE); 
new Paragraph("Blue ColorText"), HelveticaBlue); 
+0

我不想在保存文件時編輯任何東西我想要getColor或...,編輯文本.....然後將它保存爲如何在我的editTexxt上顯示它 – Eazyz

+0

您是否在使用iTextPDF生成PDF? – nnn

+0

是的它是ItextPdf版本5 – Eazyz

0

我們可以添加Properties到文件,其中包含鍵和值格式的條目。

而我們可以自己寫PropertiesFile也檢索。

像下面的代碼:

寫入屬性到文件:

private void addPropertiesToFile(String mytext){ 
     try { 
      String root = Environment.getExternalStorageDirectory().toString(); 
      File myDir = new File(root + "/CHETAN"); 
      if (!myDir.exists()) 
      myDir.mkdirs(); 
      String fname = "mytext.txt"; 
      File myFile = new File (myDir, fname); 
      if (!myFile.exists()) 
      myFile.createNewFile(); 
      Properties properties = new Properties(); 
      // set the properties value 
      properties.put("text","dsf"); 
      properties.put("textstyle","bold"); 
      properties.put("typeface","arial"); 
      properties.put("etxtColor","green"); 

      FileOutputStream fOut = new FileOutputStream(myFile); 
      OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut); 
      myOutWriter.write(mytext); 
      // save properties to project root folder 
      properties.store(myOutWriter,null); 
      myOutWriter.close(); 
      fOut.flush(); 
      fOut.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

從文件中檢索屬性:

private void retrievePropertiesFromFile(){ 
     String root = Environment.getExternalStorageDirectory().toString(); 
     File myDir = new File(root + "/CHETAN"); 
     String fname = "mytext.txt"; 
     File myFile = new File (myDir, fname); 

     InputStream input = null; 
     try { 
     input = new FileInputStream(myFile); 
     Properties prop = new Properties(); 
     // load a properties file 
     prop.load(input); 
     // get the property value and print it out 
     Log.i(getClass().getSimpleName(),prop.getProperty("text")); 
     Log.i(getClass().getSimpleName(),prop.getProperty("textstyle")); 
     Log.i(getClass().getSimpleName(),prop.getProperty("typeface")); 
     Log.i(getClass().getSimpleName(),prop.getProperty("typeface")); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } finally { 
      if (input != null) { 
       try { 
        input.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

    } 
+0

的主要代碼當我關閉應用程序重新打開它,然後打開我的文件字體回到正常的黑色當然,我添加了我的自定義int的顏色,字體和大小 – Eazyz

+0

當我添加properties.store(myOutWriter,null); 我無法得到的文本 – Eazyz

+0

其實屬性是您的文件的額外信息。所以它不適用於您的文本其公正的信息,你得到這些信息,並再次手動應用到您的文本 –

相關問題