我创建了一个onClick
函数,该函数创建折线并显示toast
。我想在点击按钮后每5秒重复一次这个功能。我该怎么做?
@Override
public void onClick(View view){
Toast.makeText(getActivity(), "Track erstellen", Toast.LENGTH_SHORT).show();
for (int e=0 ; e < points.size();e++) {
options = new PolylineOptions().width(7).color(Color.rgb(0, 139, 69)).geodesic(true);
for (int i = 0; i < points.size(); i++) {
LatLng point = points.get(i);
options.add(point);
}
line = mGoogleMap.addPolyline(options);
}
}
答案 0 :(得分:2)
重复调用它为它创建一个函数
public void onClickMethod(){
Toast.makeText(getActivity(), "Track erstellen", Toast.LENGTH_SHORT).show();
for (int e=0 ; e < points.size();e++) {
options = new PolylineOptions().width(7).color(Color.rgb(0, 139, 69)).geodesic(true);
for (int i = 0; i < points.size(); i++) {
LatLng point = points.get(i);
options.add(point);
}
line = mGoogleMap.addPolyline(options);
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
onClickMethod();
}
},5000);
}
其中5000是你的5秒延迟
并在onClick方法中调用onClickMethod();
答案 1 :(得分:0)
计时器将帮助您每5秒钟调用一次功能。
public void timer()
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@SuppressWarnings("unchecked")
public void run() {
try {
drawPloyLine();
} catch (Exception e) {
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 5000);
public void drawPloyLine(){
for (int e=0 ; e < points.size();e++) {
options = new PolylineOptions().width(7).color(Color.rgb(0,
139, 69)).geodesic(true);
for (int i = 0; i < points.size(); i++) {
LatLng point = points.get(i);
options.add(point);
}
line = mGoogleMap.addPolyline(options);
}
}