2015-08-16 51 views

回答

0

保存,你需要從你想要任何地方文件然後從你想要的任何地方讀取文件數據!

How to read and write to a file

一個字符串寫入文件:

try { 
    //use MODE_APPEND to append or MODE_PRIVATE to overwrite the file 
    FileOutputStream fos = openFileOutput("NameOfFile", Context.MODE_PRIVATE); 

    fos.write(("some text to save").getBytes()); 
    fos.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

從文件中讀取的字符串:

try { 
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(openFileInput("NameOfFile"))); 
    String inputString; 
    StringBuffer sb = new StringBuffer(); 

    //only use this while if you've saved more than one line of text 
    while ((inputString = inputReader.readLine()) !=null) { 
    sb.append(inputString + "\n"); 
    } 

    mySavedDataString = sb.toString(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
+1

同意。最好的方法是暫時將其保存在文件中並再次讀出。不再需要時刪除文件。 – Simon

0

必須在第三個活動試試這個:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

View layout1 = getActivity().getLayoutInflater().inflate(R.layout.linear_layout1, null); 
EditText tv1=(EditText)layout1 .findviewbyid(R.id.edittext1_id); 

View layout2 = getActivity().getLayoutInflater().inflate(R.layout.linear_layout2, null); 
EditText tv2=(EditText)layout2 .findViewbyid(R.id.edittext2_id); 

string tmp=tv1.getText(); 
tv1.setText=(tv2.getText()); 
tv2.setText(tmp); 
} 
+0

當然我想你想要使用第三個活動......如果你想在兩個活動中做所有事情,你必須在每個活動中使用另一個佈局的膨脹...... – sara1010

相關問題