Класс программы «Заметки» для android



public class Note extends AppCompatActivity {

EditText t;

final int cleen=1,rec=2,allapps=3,exit=4;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_note);

t=(EditText)findViewById(R.id.editText);

}

public void newNote(View view){

writeFile("\n"+getDate()+"\n"+t.getText().toString(),true);

}

void writeFile(String s,boolean b){

if (!Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED)) {

Toast.makeText(getApplicationContext(),"Память недоступна", Toast.LENGTH_SHORT).show();

return;

}

if ("".equals(t.getText().toString()))s="\n"+getDate()+"\n"+"<пустая заметка>";

File sdPath = Environment.getExternalStorageDirectory();

sdPath = new File(sdPath.getAbsolutePath() + "/" + "MyNotesReester");

sdPath.mkdirs();

File sdFile = new File(sdPath, "notes");

try {

BufferedWriter bw = new BufferedWriter(new FileWriter(sdFile,b));

bw.write(s);

bw.close();

} catch (IOException e) {

e.printStackTrace();

Toast.makeText(getApplicationContext(),"Произошла ошибка", Toast.LENGTH_SHORT).show();

return;

}

if(!"".equals(t.getText().toString()))

Toast.makeText(getApplicationContext(),"Заметка добавлена", Toast.LENGTH_SHORT).show();

else

Toast.makeText(getApplicationContext(),"Добавлена пустая заметка", Toast.LENGTH_SHORT).show();

if (!b)Toast.makeText(getApplicationContext(),"Файл перезаписан",Toast.LENGTH_SHORT).show();

}

public void showNotes(View view){

startActivity(new Intent(Note.this,Show.class));

}

private String getDate() {

SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");

return dateFormat.format(new Date());

}

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.notebook, menu);

menu.add(0,cleen,0,"Очистить");

menu.add(0,rec,0,"Перезаписать");

menu.add(0,allapps,0,"Приложения");

menu.add(0,exit,0,"ВЫХОД");

return true;

}

public boolean onOptionsItemSelected(MenuItem item){

switch (item.getItemId()){

case cleen:

t.setText("");

break;

case rec:

writeFile("\n"+getDate()+"\n"+t.getText().toString(),false);

break;

case allapps:

try {

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setData(Uri.parse("market://search?q=developer%20kalexal&hl=ru"));

startActivity(intent);

}catch (Exception e){

Toast.makeText(getApplicationContext(),"Не найдено ни одного маркета", Toast.LENGTH_LONG).show();

}

break;

case exit:

finish();

break;

}

return super.onOptionsItemSelected(item);

}

}


Дата добавления: 2019-02-12; просмотров: 114; Мы поможем в написании вашей работы!

Поделиться с друзьями:






Мы поможем в написании ваших работ!