Programming Language/Android Programming
내장메모리 접근
1. 레이아웃 만들기
1) activity_main.xml 설정
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btnWrite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="내장메모리에 파일쓰기" />
<Button
android:id="@+id/btnRead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="내장메모리에서 파일 읽기" />
</LinearLayout>
▶ 위와 같이 activity_main.xml을 만들어준다.
2) MainActivity.java
package com.example.d4tai1.pro20181022a_2;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tvName, tvEmail;
Button btnDialog;
View dlgView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvName = (TextView)findViewById(R.id.tvName);
tvEmail = (TextView)findViewById(R.id.tvEmail);
btnDialog = (Button)findViewById(R.id.button1);
btnDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dlgView = View.inflate(MainActivity.this, R.layout.dialog1, null);
//가지고 들어온다.(가지고 들어올 곳, 이 뷰에 가지고 들어올 xml파일, 뷰그룹)
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setView(dlgView);
dlg.setIcon(R.drawable.ic_launcher_background);
dlg.setTitle("사용자 정보 입력");
dlg.setMessage("사용자 정보 입니다.");
dlg.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText edtName = (EditText)dlgView.findViewById(R.id.edtDlg1);
EditText edtEmail = (EditText)dlgView.findViewById(R.id.edtDlg2);
//그냥 findViewById()를 하게되면 엑티비티메인의 것을 가져오는 것을 말하기 때문에
tvName.setText(edtName.getText().toString());
tvEmail.setText(edtEmail.getText().toString());
}
});
dlg.show();
}
});
}
}
3) 시연
[1] 기본설정
▶ 위와 같이 구성한다.
[2] 파일쓰기
▶ 파일쓰기를 누를 경우 file.txt 파일이 생성된다.
[3] 파일 읽기
▶ 파일 읽기를 누르면 Toast메세지에 안드로이드라고 출력되는 것을 확인할 수 있다.
'Programming Language > Android Programming' 카테고리의 다른 글
| Application 제작 (0) | 2018.11.26 |
|---|---|
| 일기장 제작 (0) | 2018.11.05 |
| 대화상자[입력] (0) | 2018.10.27 |
| 대화상자[라디오] (0) | 2018.10.27 |
| 메세지 띄우기 (0) | 2018.10.09 |
댓글