我正在尝试为显示图像或按钮的片段实现 Click 事件。
我能够实现翻转视图以获取另一个片段,但我无法让单击事件在此类片段上起作用。
我尝试过使用 android:onClick,甚至设置clickable
为true
. 我什至尝试过使用 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>
是否有某种原因,翻转视图中的片段不响应单击或触摸事件,或者我的代码中是否有问题?