我有一个名为“保存”的按钮。 当我点击它时,它将调用以下编码:
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);}
}}
答案 0 :(得分:1)
您需要在班级中保留按钮点击的计数器。由于您没有指定您的班级名称,我只需将其命名为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(View.INVISIBLE)
隐藏它。
答案 1 :(得分:0)
设置一个实例变量来保存点击并设置一个onclick监听器来增加该变量。然后一旦达到该数量,就更改按钮属性。
如果您需要进一步的帮助,我们需要看全班向您展示。
答案 2 :(得分:0)
将其添加到点击侦听器的顶部。
private int numClicks = 0;
public void SaveText(View view){
numClicks++;
if(numClicks == 10) {
view.setEnabled(false);
}
答案 3 :(得分:0)
创建一个在onclick结束时递增的变量。在onclick中,在增量之前,创建一个条件来检查你的变量是否等于9.如果它等于9,则禁用该按钮。
编辑:
int counter = 0;
public void SaveText()
{
doSomething();
if(counter == 9)
{
myButton.setEnabled(false);
}
counter++;
}