Вот код:
Код: Выделить всё
public class Fragment1 extends Fragment implements qwerty, View.OnClickListener {
private final static String FILENAME = "sample.txt"; // имя файла
private EditText mEditText;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save:
saveFile(FILENAME);
return true;
case R.id.action_settings:
Intent intent = new Intent();
intent.setClass(this, SettingsActivity.class);
startActivity(intent);
return true;
default:
return true;
}
}
private void saveFile(String fileName) {
try {
OutputStream outputStream = openFileOutput(fileName, 0);
OutputStreamWriter osw = new OutputStreamWriter(outputStream);
osw.write(mEditText.getText().toString());
osw.close();
} catch (Throwable t) {
Toast.makeText(getApplicationContext(),
"Exception: " + t.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onResume() {
super.onResume();
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
// читаем размер шрифта из EditTextPreference
float fSize = Float.parseFloat(prefs.getString(
getString(R.string.pref_size), "20"));
// применяем настройки в текстовом поле
mEditText.setTextSize(fSize);
// читаем стили текста из ListPreference
String regular = prefs.getString(getString(R.string.pref_style), "");
int typeface = Typeface.NORMAL;
if (regular.contains("Полужирный"))
typeface += Typeface.BOLD;
if (regular.contains("Курсив"))
typeface += Typeface.ITALIC;
mEditText.setTypeface(null, typeface);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
Код: Выделить всё
private void saveFile(String fileName) {
try {
OutputStream outputStream = openFileOutput(fileName, 0);
OutputStreamWriter osw = new OutputStreamWriter(outputStream);
osw.write(mEditText.getText().toString());
osw.close();
} catch (Throwable t) {
}
}