Android被逼学习小例子1
这个程序的功能就是当点击图片的时候,就会自动切换到下一个图片。对,就是这么简单的一个功能,高手请不要鄙视。
主要的代码如下:
HelloWorldActivity.java代码为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package hello.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout;
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
int [] images = new int [] {
R.drawable.ajax, R.drawable.java, R.drawable.ee, R.drawable.classic,
R.drawable.xml, };
int currentImage = 0 ;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.root);
final ImageView image = new ImageView( this );
mainLayout.addView(image);
image.setImageResource(images[ 0 ]);
image.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
if (currentImage >= 4 ) {
currentImage = - 1 ;
}
image.setImageResource(images[++currentImage]);
}
});
}
} |
main.xml代码为:
1
2
3
4
5
6
7
|
<? xml version="1.0" encoding="utf-8"?>
android:id="@+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</ LinearLayout >
|
其余的没多大变化,只是图片放在drawable-mdpi目录下面。
运行结果为: