我有一个名为“保存”的按钮。当我单击它时,它将调用以下编码:
public void SaveText(View view){
try {
OutputStreamWriter out = new OutputStreamWriter(openFileOutput ("myfilename.txt",MODE_APPEND));
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
String latt = Double.toString(latitude);
String lonn = Double.toString(longitude);
String text =(latt+" "+lonn+" "+mydate);
out.write(text);
out.write('\n');
out.close();
Toast.makeText(this,"Text Saved !",Toast.LENGTH_LONG).show();
}
catch (java.io.IOException e) {
//do something if an IOException occurs.
Toast.makeText(this,"Sorry Text could't be added",Toast.LENGTH_LONG).show
();
}
当用户已经单击它 10 次时,我想将“保存”按钮设置为不可单击,这样它就不会将超过 10 个文本保存到文本文件中。
编辑
到目前为止我所尝试的:
做{
try {
OutputStreamWriter out = new OutputStreamWriter(openFileOutput ("myfilename.txt",MODE_APPEND));
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
String latt = Double.toString(latitude);
String lonn = Double.toString(longitude);
String text =(latt+" "+lonn+" "+mydate);
out.write(text);
out.write('\n');
out.close();
Toast.makeText(this,"Text Saved !",Toast.LENGTH_LONG).show();
count++;
}
catch (java.io.IOException e) {
Toast.makeText(this,"Sorry Text could't be added",Toast.LENGTH_LONG).show
();
}}
while(count<9);
if (count>9){Button btn=(Button)findViewById(R.id.Save); btn.setEnabled(false);}
}
结果是当单击“保存”按钮一次时,它会禁用我的“保存”按钮,而无需等待它被单击 10 次。
新代码
public void SaveText(View view){
if (this.counter <= 10) {
try {
OutputStreamWriter out = new OutputStreamWriter(openFileOutput ("xy.txt",MODE_APPEND));
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
String latt = Double.toString(latitude);
String lonn = Double.toString(longitude);
String text =(latt+" "+lonn+" "+mydate);
out.write(text);
out.write('\n');
out.close();
Toast.makeText(this,"Text Saved !",Toast.LENGTH_LONG).show();
}
catch (java.io.IOException e) {
Toast.makeText(this,"Sorry Text could't be added",Toast.LENGTH_LONG).show
();
}
this.counter++; }
}}
我尝试过的所有方法,但是当我第二次单击“保存”按钮时,它什么也不做。
解决方案 :
非常感谢。大家真的帮我解决了这个问题。
public void SaveText(View view){
if (this.counter <= 10) {
//to-do coding
}
this.counter++;
}
else{Button btn=(Button)findViewById(R.id.Save); btn.setEnabled(false);}
}}