Demo Lesson 5
Demo Lesson 5
OnClickListener
findViewById(R.id.btnwriteIS).setOnClickListener(this);
findViewById(R.id.btnreadIS).setOnClickListener(this);
findViewById(R.id.btnwriteFC).setOnClickListener(this);
findViewById(R.id.btnreadFC).setOnClickListener(this);
findViewById(R.id.btnwriteES).setOnClickListener(this);
findViewById(R.id.btnreadES).setOnClickListener(this);
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnreadIS:
String readDataIS = readIS("myfile.txt");
Toast.makeText(this, "Nôi dung file:" + readDataIS, Toast.LENGTH_SHORT).show();
break;
case R.id.btnwriteIS:
writeIS("myfile.txt","Xin chào các bạn!");
break;
case R.id.btnreadFC:
String readDataFC = readFC("mycached.cache");
Toast.makeText(this, "Data cache: " + readDataFC, Toast.LENGTH_SHORT).show();
break;
case R.id.btnwriteFC:
writeFC("mycached.cache","Dữ liệu lưu vào cahce");
break;
case R.id.btnwriteES:
writeES("mysdcard.txt","Dữ liệu lưu vào thẻ nhớ ngoài");
break;
case R.id.btnreadES:
String readDataEX = readES("mysdcard.txt");
Toast.makeText(this, "Data sdcard: " + readDataEX, Toast.LENGTH_SHORT).show();
break;
}
}
private String readIS(String filename) {
try (FileInputStream fin = openFileInput(filename))
{
byte[] buffer = new byte[1024];
int length = 0;
length = fin.read(buffer);
return new String(buffer,0,length); private void writeIS(String filename,String data) {
try {
OutputStreamWriter writer = new OutputStreamWriter(new
FileOutputStream(sdcard));
writer.write(data);
writer.close();
Toast.makeText(this, "Lưu thành công", Toast.LENGTH_SHORT).show(); private String readES(String filename) {
} catch ( Exception ex )
{ String sdcard = getExternalFilesDir("") + "/" + filename;
ex.printStackTrace(); try {
}
Scanner scan = new Scanner(new File(sdcard));
}
String data="";
while (scan.hasNext()){
data = data + scan.nextLine() + "\n";
}
scan.close();
return data;
} catch ( Exception ex )
{
ex.printStackTrace();
}
public class LoginActivity extends AppCompatActivity implements View.OnClickListener
findViewById(R.id.btnLogin).setOnClickListener(this);
findViewById(R.id.btnThoat).setOnClickListener(this);
edtUsername = findViewById(R.id.edtUsername);
edtPassword = findViewById(R.id.edtPassword);
ckRememberPassword = findViewById(R.id.ckRememberPassword);