-2

我正在尝试为显示图像或按钮的片段实现 Click 事件。

我能够实现翻转视图以获取另一个片段,但我无法让单击事件在此类片段上起作用。

我尝试过使用 android:onClick,甚至设置clickabletrue. 我什至尝试过使用 onClickListener 和 onTouch,但它并没有为我开火。

这是我尝试过的代码。

public class MainActivity extends Activity {
private FlipViewGroup contentView;
public int currentimageindex=0;
 private GestureDetector mDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);
        // hde staitbar otef Andoroid
        // could also be usdne lar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setTitle(R.string.activity_title);
    contentView = new FlipViewGroup(this);
    contentView.addFlipView(View.inflate(this, R.layout.second_page, null));
    contentView.addFlipView(View.inflate(this, R.layout.first_page, null)); 
    setContentView(contentView);
//  setContentView(R.layout.second_page);
    contentView.startFlipping(); //make the first_page view flipping        
    ImageView imgFavorite =   (ImageView) findViewById(R.id.imageView1);
    imgFavorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,
                    "The favorite list would appear on clicking this icon",
                    Toast.LENGTH_LONG).show();
        }
    });      
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

}
@Override
protected void onResume() {
    super.onResume();
    contentView.onResume();
}
@Override
protected void onPause() {
    super.onPause();
    contentView.onPause();
}   
public void onclick(View v){
    ImageView imgFavorite =   (ImageView) findViewById(R.id.imageView1);
    imgFavorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,
                    "The favorite list would appear on clicking this icon",
                    Toast.LENGTH_LONG).show();
        }
    }); 
}
}

这是我的 xml 代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/white">                
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:src="@drawable/android"
android:clickable="true"
android:onClick="onclick"/>    
</RelativeLayout>

是否有某种原因,翻转视图中的片段不响应单击或触摸事件,或者我的代码中是否有问题?

4

1 回答 1

0

据我了解, FlipViewGroup 是您的自定义视图组实现。如果是这种情况,您需要 onInterceptTouchEvent() 请参阅此处的示例代码。 http://developer.android.com/training/gestures/viewgroup.html

于 2013-09-24T10:21:55.967 回答