- 다음, 이전버튼을 만든다.
- 5개를 복사하고 (res/drawable_mdpi폴더) 이미지 보이기 예제
(1) activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/dog1"
android:layout_gravity="center_horizontal"
/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:id="@+id/preBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prev"
android:layout_weight="1"
/>
<Button android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
(2) MainActivity.java
; 버튼 이벤트 적용
2개버튼(이전,다음)의 이벤트가 동일하기 때문에 리스너 연결함.
( button1.setOnClickedListener 식으로 버튼마다 생성해도 되나 동일한 이벤트이므로.. )
; R.drawabled.##로 배열에 참조값 가져오기.
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
int cnt=0;
int imgs[] = {R.drawable.dog1,R.drawable.dog2,R.drawable.dog3,R.drawable.dog4,R.drawable.dog5};
Button prevBtn;
Button nextBtn;
ImageView img1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prevBtn = (Button)findViewById(R.id.preBtn);
nextBtn = (Button)findViewById(R.id.nextBtn);
img1 = (ImageView)findViewById(R.id.img);
prevBtn.setOnClickListener(mm);
nextBtn.setOnClickListener(mm);
}
@Override
public void onClick(View v) {
Log.i("logMsg", "index:" + cnt);
switch(v.getId()){
case R.id.preBtn:
if(cnt>0){
img1.setImageResource(imgs[--cnt]);
}
break;
case R.id.nextBtn:
if(cnt<imgs.length -1){
img1.setImageResource(imgs[++cnt]);
}
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
'Mobile > Android' 카테고리의 다른 글
#09 ( FrameLayout ) (0) | 2013.02.02 |
---|---|
#08 ( layout을 만들고 연결하기) (0) | 2013.02.02 |
#06 (이미지 뷰) (0) | 2013.02.02 |
#05 ( checkbox, radiobutton ) (0) | 2013.01.29 |
#04( 입력한 값 보여주기 예제 ) (0) | 2013.01.28 |