달력

4

« 2024/4 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
2011. 8. 30. 15:26

EditText 에 입력하여 file로 저장하기 Study/Android2011. 8. 30. 15:26

public class MyFile extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//사용할 것들
EditText edit;
Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//findViewById를 이용해 id 값을 불러옴
edit = (EditText) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.button);

//button을 클릭하면 이벤트 발생
btn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

//id 값을 가져와서 R.id.button 값일 경우 실행
switch(v.getId()){
case 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();
}
}

}
}

'Study > Android' 카테고리의 다른 글

지도기능 이용하기  (0) 2011.09.01
file저장기법+OptionMenu+dialog 어차피 OptionMenu부분만 추가되었음..  (0) 2011.08.31
File저장기법+dialog를 이용해 보기  (0) 2011.08.31
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
:
Posted by 유쾌한순례자