読者です 読者をやめる 読者になる 読者になる

name_untitledのエッセイ

IT関係で知ったことなどを記事としてあげていきます。内容に不備や質問などあればぜひコメントをよせてください。

Android メモアプリ作成時の参考サイト。

演習用にメモアプリ作った。

こんなの。

f:id:name_untitled:20160313021903p:plain f:id:name_untitled:20160313021908p:plain

 

 

MainActivity.java
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), EditActivity.class);
startActivity(intent);
}
});

setListView();
}

private void setListView() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(),R.layout.row, getFilesDir().list());
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent
, View view, int position, long id) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(), EditActivity.class);
String title = ((TextView)view.findViewById(R.id.tvTitle)).getText().toString();
intent.putExtra("TITLE",title);
startActivity(intent);

}
});

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
protected void onResume(){
super.onResume();
setListView();
}
}


EditActivity.java
public class EditActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Intent intent = getIntent();
String title = getIntent().getStringExtra("TITLE");
((EditText)findViewById(R.id.editText)).setText(title);
((EditText)findViewById(R.id.editText2)).setText(getContent(title));
}

private String getContent(String fileName){
if(fileName == null) return null;
StringBuilder sb = new StringBuilder();
try{
FileInputStream in = openFileInput(fileName);
BufferedReader reader = new BufferedReader( new InputStreamReader( in , "UTF-8") );
String tmp;
while( (tmp = reader.readLine()) != null ){
sb.append(tmp).append("\n");
}
reader.close();
}catch( IOException e ){
e.printStackTrace();
}
return sb.toString();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_edit, menu);
return true;
}

protected void saveText(){
String message = "";
String fileName = ((EditText)findViewById(R.id.editText)).getText().toString();
String inputText = ((EditText)findViewById(R.id.editText2)).getText().toString();
try {
FileOutputStream outStream = openFileOutput(fileName, MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(outStream);
writer.write(inputText);
writer.flush();
writer.close();
message = "File "+fileName+ " saved.";
} catch (FileNotFoundException e) {
message = e.getMessage();
e.printStackTrace();
} catch (IOException e) {
message = e.getMessage();
e.printStackTrace();
}
Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_save) {
saveText();
return true;
}
if (id == R.id.action_delete){
deleteText();
return true;
}
if (id == R.id.action_share){
shareText();
return true;
}

return super.onOptionsItemSelected(item);
}

private void shareText() {
ShareCompat.IntentBuilder builder =
ShareCompat.IntentBuilder.from(EditActivity.this);

builder.setSubject(
((EditText)findViewById(R.id.editText)).getText().toString());
builder.setText(
((EditText)findViewById(R.id.editText2)).getText().toString());

builder.setType("text/plain");
builder.startChooser();
}

private void deleteText() {
String fileName = ((EditText)findViewById(R.id.editText)).getText().toString();
deleteFile(fileName);
Toast.makeText(getApplicationContext(), "File "+fileName+" deleted", Toast.LENGTH_LONG).show();
finish();
}
}
 

リストだけ挙げる。ありがとうございました。

it-trick-java.appspot.com

thr3a.hatenablog.com

techbooster.org

android.roof-balcony.com

android.keicode.com

web-terminal.blogspot.com

blog.oukasoft.com

android.keicode.com

dev.classmethod.jp