I am also quite new to Android, but I will give him a chance.
, , , , , , "". , false true. , "for" , , . .
XML :
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start"
android:Clickable="true"
android:onClick="start"
/>
<Button
android:id="@+id/stop" <!-- Gives the button an ID to use in java code -->
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop"
android:Clickable="true"
android:onClick="stop"
/>
2 ImageViews: blue_rectangle red_rectangle, . XML ImageViews
<ImageView
android:id="@+id/blue_rectangle"
android:layout_width="100dp"
android:layout_height="75dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="5dp"
android:src="@drawable/blue_rectangle" />
<ImageView
android:id="@+id/red_rectangle"
android:layout_width="100dp"
android:layout_height="75dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="5dp"
android:src="@drawable/red_rectangle" />
- .
Java.
package your.package.name.thingy.here;
public class BlinkingActivity extends Activity{
ImageView blueRectangle;
ImageView redRectangle;
Button start;
Button stop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
blueRectangle = (ImageView) findViewById(R.id.blue_rectangle);
redRectangle = (ImageView) findViewById(R.id.red_rectangle);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
Boolean isBlinking = new Boolean(false);
blinkRectangle(whateverVariableThisNeeds);
}
public static void blinkRectangle(View view){
blueRectangle.setVisibility(View.INVISIBLE);
redRectangle.setVisibility(View.INVISIBLE);
for(initialization; isBlinking; update){
blueRectangle.setVisibility(View.VISIBLE);
blueRectangle.postDelayed(new Runnable() {
@Override
public void run() {
blueRectangle.setVisibility(View.INVISIBLE);
}
}, 2000);
redRectangle.setVisibility(View.VISIBLE);
redRectangle.postDelayed(new Runnable() {
@Override
public void run(){
redRectangle.setVisibility(View.INVISIBLE);
}
}, 2000);
blueRectangle.setVisibility(View.VISIBLE);
}
public static void start(View view){
isBlinking = true;
}
public static void stop(View view){
isBlinking = false;
}
}
.
, false. , . , for blinkRectangle(). , for , 2 , , , . start() stop() true false . , for , . . , -, .
, , . , - , , , , , . , !
!
P.S. ,
http://www.tutorialspoint.com/java/java_loop_control.htm
http://www.java-examples.com/java-boolean-example
... , 2 . , , !