我有一个名为“保存”的按钮。 当我点击它时,它会调用下面的代码:
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);}
}}
最佳答案
您需要在您的类(class)中保留按钮点击的计数器。由于您没有指定您的类(class)名称,我将其命名为 YourClass
。
public class YourClass ... {
private int buttCounter = 0;
...
}
然后您需要在单击按钮时更新并检查此计数器。
public void SaveText(View view){
if (this.buttCounter <= 10) {
// Your old method body goes here
this.buttCounter++;
}
}
这不会停用按钮,但它不会在按钮按下时执行任何操作。如果您想禁用该按钮,您可以使用 Button.setClickable(false)
、Button.setEnabled(false)
甚至使用 Button.setVisibility(查看.INVISIBLE)
.
关于java - 当用户已经点击一个按钮10次时,如何设置点击按钮的限制-android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17656332/