goto statement in Java
You can use for, do while, while cycle for example:
public void myFunction(){
for (int i = 0; i < 1; i++) {
// some code
int c = 10;
if(c==10)
break; // goto stop; in C++
} // end of for
// stop: // break moved process to end of for
// next code
}
396LW NO topic_id
AD
Další témata ....(Topics)
Update TextView from TimerTask, Handler, schedule, run, cancel TimerTask, Android example
public class TimerActivity extends Activity {
TimerTask mTimerTask;
final Handler handler = new Handler();
Timer t = new Timer();
TextView hTextView;
TableRow hTableRow;
Button hButton, hButtonStop;
private int nCounter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hTextView = (TextView)findViewById(R.id.idTextView);
hButton = (Button)findViewById(R.id.idButton);
hButton.setOnClickListener(mButtonStartListener);
hButtonStop = (Button)findViewById(R.id.idButtonStop);
hButtonStop.setOnClickListener(mButtonStopListener);
} // end onCreate
View.OnClickListener mButtonStartListener = new OnClickListener() {
public void onClick(View v) {
doTimerTask();
}
};
View.OnClickListener mButtonStopListener = new OnClickListener() {
public void onClick(View v) {
stopTask();
}
};
public void doTimerTask(){
mTimerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
nCounter++;
// update TextView
hTextView.setText("Timer: " + nCounter);
Log.d("TIMER", "TimerTask run");
}
});
}};
// public void schedule (TimerTask task, long delay, long period)
t.schedule(mTimerTask, 500, 3000); //
}
public void stopTask(){
if(mTimerTask!=null){
hTextView.setText("Timer canceled: " + nCounter);
Log.d("TIMER", "timer canceled");
mTimerTask.cancel();
}
}
}
Date difference in millisecond between two date, GregorianCalendar, Calendar, after(), befor(), getTimeInMillis().
public class HoriziontalScrollActivity extends Activity {
TextView txtV;
Context cntx;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtV = (TextView)findViewById(R.id.idLabel);
cntx = this;
StringBuilder strBuild = new StringBuilder();
Calendar firstDate = new GregorianCalendar(2011, Calendar.DECEMBER, 31);
Calendar secondDate = new GregorianCalendar(2012, Calendar.JANUARY, 1);
Boolean bDetermine = firstDate.after(secondDate); // false
strBuild.append("Is firsDate after secondDate? " + bDetermine + "
");
bDetermine = firstDate.before(secondDate); // true
strBuild.append("Is firsDate before secondDate? " + bDetermine + "
");
long differenceInMillisecond = 0L;
differenceInMillisecond = secondDate.getTimeInMillis()-firstDate.getTimeInMillis();
// second == 1000 millisecond
long second = differenceInMillisecond / 1000L;
strBuild.append("Difference between two dates is: " + second + "
");
txtV.setText(strBuild);
}
}
public class MyClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
String sMyText = "some text";
int nMyInt = 10;
@Override
protected void onSaveInstanceState(Bundle outState) {
// Save away the original text, so we still have it if the activity
// needs to be killed while paused.
outState.putString("my_text", sMyText);
outState.putInt("my_int", nMyInt);
Toast.makeText(this, "onSaveInstanceState()", Toast.LENGTH_LONG).show();
Log.i("onSaveInstanceState", "onSaveInstanceState()");
}
String sNewMyText = "";
int nNewMyInt = 0;
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// restore saved values
sNewMyText = savedInstanceState.getString("my_text");
nNewMyInt = savedInstanceState.getInt("my_int");
Toast.makeText(this, "onRestoreInstanceState()", Toast.LENGTH_LONG).show();
Log.i("onRestoreInstanceState", "onRestoreInstanceState()");
}
}
TimerTask with updating of TextView here
//android.okhelp.cz/asynctask-example-android-with-progressbar/
//android.okhelp.cz/timer-task-timertask-run-cancel-android-example/
package cz.okhelp.timer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TimerActivity extends Activity {
TextView hTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hTextView = (TextView)findViewById(R.id.idTextView);
MyTimerTask myTask = new MyTimerTask();
Timer myTimer = new Timer();
// public void schedule (TimerTask task, long delay, long period)
// Schedule a task for repeated fixed-delay execution after a specific delay.
//
// Parameters
// task the task to schedule.
// delay amount of time in milliseconds before first execution.
// period amount of time in milliseconds between subsequent executions.
myTimer.schedule(myTask, 3000, 1500);
}
class MyTimerTask extends TimerTask {
public void run() {
// ERROR
hTextView.setText("Impossible");
// how update TextView in link below
// //android.okhelp.cz/timer-task-timertask-run-cancel-android-example/
System.out.println("");
}
}
}
//android.okhelp.cz/asynctask-example-android-with-progressbar/
//android.okhelp.cz/timer-task-timertask-run-cancel-android-example/
If you do click on Rebuid project menu item, this operation maybe consume long time on slovly PC.
Some errors dismis when you do rebuild project, or module.
Try this:
Select module where contains red error code and press Debugg or Run button on Android Studio.
If module code contains errors, Android Studio show error report in gradle console.
If not, apk will installed on device.
This is 10 times faster than Clean - Rebuild all project.
Some errors dismis when you do rebuild project, or module.
Try this:
Select module where contains red error code and press Debugg or Run button on Android Studio.
If module code contains errors, Android Studio show error report in gradle console.
If not, apk will installed on device.
This is 10 times faster than Clean - Rebuild all project.
Editace: 2012-12-08 10:02:33
Počet článků v kategorii: 396
Url:goto-statement-in-java