如何为视图翻板中的每个图像做烤面包?我的意思是我希望每次点击下一个按钮,就会出现一个吐司并告诉它的第一张照片。 ,第2张照片,第3张照片,依此类推...... 这是mah代码: main.java
public class main extends Activity implements View.OnClickListener
{
ViewFlipper viewFlipper;
Button next;
Button previous;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == next)
viewFlipper.showNext();
else if (v == previous) {
viewFlipper.showPrevious();
}
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="z.a.q.main"
android:layout_centerInParent="true"
android:background="@android:color/black" >
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/viewFlipper"
android:layout_centerInParent="true">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:id="@+id/imageView"
android:src="@drawable/share"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:id="@+id/imageView2"
android:src="@drawable/rating"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:id="@+id/imageView3"
android:src="@drawable/rate"/>
</ViewFlipper>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<Button
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignTop="@+id/next"/>
</RelativeLayout>
请。帮助..
答案 0 :(得分:3)
使用视图ID来识别它。
使用吐司并在文中使用
viewFlipper.indexOfChild(viewFlipper.getCurrentView())
它将返回CURRENT显示视图的INDEX。
@Override
public void onClick(View v) {
int viewId = v.getId();
if (viewId == R.id.next){
viewFlipper.showNext();
Toast.makeText(getApplicationContext(),
"IMAGE - "+viewFlipper.indexOfChild(viewFlipper.getCurrentView())
,Toast.LENGTH_LONG).show();
}
else if (v == R.id.previous) {
viewFlipper.showPrevious();
Toast.makeText(getApplicationContext(),
"IMAGE - "+viewFlipper.indexOfChild(viewFlipper.getCurrentView())
,Toast.LENGTH_LONG).show();
}
}
&#13;
答案 1 :(得分:0)
最简单的方法是在活动中添加counter
成员变量,只需在翻转图像时继续递增计数器。
显示Toast的代码是,
Toast.makeText(getApplicationContext(),YOUR_MESSAGE,duration).show();
或者只需使用以下代码替换您的活动代码
public class main extends Activity implements View.OnClickListener
{
ViewFlipper viewFlipper;
Button next;
Button previous;
int counter = 1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == next)
{
viewFlipper.showNext();
counter++;
Toast.makeText(getApplicationContext()," Picture "+counter,Toast.LENGTH_SHORT).show();
}
else if (v == previous)
{
viewFlipper.showPrevious();
counter--;
Toast.makeText(getApplicationContext()," Picture "+counter,Toast.LENGTH_SHORT).show();
}
}
}