2011. 8. 31. 08:47
File저장기법+dialog를 이용해 보기 Study/Android2011. 8. 31. 08:47
package com.narratage.com;
import java.io.FileOutputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MyFile extends Activity {
/** Called when the activity is first created. */
// 사용할 것들
EditText edit;
Button btn;
static final int dialog_id0 = 0;
String txt;
AlertDialog dlg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// findViewById를 이용해 id 값을 불러옴
btn = (Button) findViewById(R.id.button);
}
public void onResume() {
super.onResume();
edit = (EditText) findViewById(R.id.editText);
// button의 OnClickListener 이벤트
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//dialog를 보여줌
showDialog(dialog_id0);
}
});
}
@Override
//맨처음 만들어질 다이얼로그
protected Dialog onCreateDialog(int id) {
super.onCreateDialog(id);
System.out.println("oncreatedialog");
switch (id) {
case dialog_id0:
dlg = new AlertDialog.Builder(this)
//AlertDialog의 아이콘
.setIcon(R.drawable.icon)
//AlertDialog의 타이틀
.setTitle("저장내용")
//AlertDialog의 메세지 내용 단 최초 한번은 여기서 부르나
//다음부터는 onPrepareDialog 메소드에서 재정의함
//다음부터는 onPrepareDialog 메소드에서 재정의함
.setMessage(edit.getText().toString())
//Ok를 누르면 발생할 이벤트
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
//입력받은 값이 있는지 없는지를 판단하여 fail 이나
//save 메소드를 호출함.
//save 메소드를 호출함.
// == 이것으로 비교할 경우 논리오류가 발생할
// 우려가 있음(해보진않음)
// 우려가 있음(해보진않음)
if (edit.getText().toString().equals("")) {
fail();
} else {
save();
}
}
})
//cancel을 누르면 dialog 닫힘
.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
}).create();
break;
default:
dlg = null;
}
return dlg;
}
@Override
//두번째부터 재정의하게 될 onPrepareDialog 여기서 메세지를 재정의해줌
public void onPrepareDialog(int id, Dialog dlg) {
super.onPrepareDialog(id, dlg);
this.dlg.setMessage(edit.getText().toString());
}
public void save() {
// id 값을 가져와서 R.id.button 값일 경우 실행
try {
// 입력받은 값을 String 형으로 저장함
String txt = edit.getText().toString();
// test.txt 파일을 쓰기 권한으로 줌
FileOutputStream outstream = openFileOutput("test.txt",
Activity.MODE_WORLD_WRITEABLE);
// byte 단위로 입력함
outstream.write(txt.getBytes());
// Exception이 발생하지 않았으므로 저장완료 Toast를 띄움
Toast.makeText(this, "저장완료!!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
// Exception이 발생하였으므로 저장실패 Toast를 띄움
Toast.makeText(this, "저장실패!", Toast.LENGTH_LONG).show();
}
}
public void fail() {
Toast.makeText(this, "아무것도 입력되지 않았어요!", Toast.LENGTH_LONG).show();
}
}
'Study > Android' 카테고리의 다른 글
지도기능 이용하기 (0) | 2011.09.01 |
---|---|
file저장기법+OptionMenu+dialog 어차피 OptionMenu부분만 추가되었음.. (0) | 2011.08.31 |
EditText 에 입력하여 file로 저장하기 (0) | 2011.08.30 |
Data Storage 中 Preferences (0) | 2011.08.30 |
TextView 그림자처리와 자동링크 (0) | 2011.08.30 |
px (vs) dp (0) | 2011.08.29 |
상태바 Notification 예제 파일분석 (0) | 2011.08.29 |
Intent (0) | 2011.08.29 |
getSystemService (0) | 2011.08.29 |
Style / Theme (0) | 2011.08.29 |