【发布时间】:2017-01-31 15:30:29
【问题描述】:
正在处理Android Version - KITKAT。当Button - newRide 没有触发onClick() 方法时问题仍然存在,相同的代码在其他Android Version > KITKAT 上运行良好。
来自我的班级的 Java 代码片段:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_ongoing_ride_details, null, false);
View cView = getLayoutInflater().inflate(R.layout.title_bar, null);
imgSignOut = (ImageView) cView.findViewById(R.id.imgSignout);
imgRefresh = (ImageView) cView.findViewById(R.id.imgRefresh);
tvTitle = (TextView) cView.findViewById(R.id.tvTitle);
newRide = (Button)contentView.findViewById(R.id.btnNewRide);
if (Utilities.getStringFromPrefs(getApplicationContext(), "userType")
.equals("driver")) {
setContentView(contentView);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(cView);
actionBar.setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#ffffff")));
tvTitle.setText("ON GOING RIDES");
tvTitle.setTextColor(Color.BLACK);
}
else {
frameLayout.addView(contentView);
getActionBar().setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM
| android.support.v7.app.ActionBar.DISPLAY_SHOW_HOME | android.support.v7.app.ActionBar.DISPLAY_HOME_AS_UP);
getActionBar().setCustomView(R.layout.custom_title);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);
getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#ffffff")));
TextView headTxt = (TextView) findViewById(R.id.mytext);
headTxt.setTextColor(Color.BLACK);
headTxt.setText("ON GOING RIDES");
}
lvRideList = (ListView) findViewById(R.id.lvRideDetails);
llNoBookings = (LinearLayout) findViewById(R.id.llNoBookings);
HsApi = new APIImple();
onGoingRideResult = new ArrayList<OnGoingRideDetails>();
mTask = new GetOnGoingRideDetails().execute();
if (!(Utilities.getStringFromPrefs(getApplicationContext(), "userType")
.equals("driver"))) {
newRide.setVisibility(View.VISIBLE);
newRide.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!Utilities.isConnected(getApplicationContext())) {
Utilities.notConnectedToast(getApplicationContext());
} else {
Intent in = new Intent(OnGoingRideDetailsList.this,
FragmentActivity.class);
startActivity(in);
}
}
});
}
else {
newRide = (Button) findViewById(R.id.btnNewRide);
newRide.setVisibility(View.GONE);
}
lvRideList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if (onGoingRideResult.get(position).getUserType()
.equals("driver")) {
Intent intentToDriverScreen = new Intent(
OnGoingRideDetailsList.this,
DriverWelcomeScreen.class);
intentToDriverScreen.putExtra("rideData",
onGoingRideResult.get(position));
startActivity(intentToDriverScreen);
}
else {
Intent intentToUserRideScreen = new Intent(
OnGoingRideDetailsList.this,
UserCurrentBookings.class);
intentToUserRideScreen.putExtra("rideData",
onGoingRideResult.get(position));
startActivity(intentToUserRideScreen);
}
}
});
}
XMLactivity_ongoing_ride_details.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:background="@color/bgcolor"
android:descendantFocusability="blocksDescendants">
<Button
android:id="@+id/btnNewRide"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:background="@drawable/mybutton_bg"
android:clickable="true"
android:text="Click here for New Ride"
android:textColor="@color/buttontextcolor"
android:textSize="15dp"
android:visibility="visible"/>
<ListView
android:id="@+id/lvRideDetails"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#c8c8c8"
android:dividerHeight="1dp"
android:focusable="false"/>
<LinearLayout
android:id="@+id/llNoBookings"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:padding="10dp"
android:text="Sorry! No Active Bookings Available for you"
android:textColor="#ff9966"
android:textStyle="bold"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/sadsmiley"/>
</LinearLayout>
</RelativeLayout>
【问题讨论】:
-
@W4R10CK 我很乐意帮助 OP。就在这里做吧。没有必要有多个相同的问题。老实说,我上次看到 OP 从你们那里得到帮助后才留下这个问题。
-
快速浏览一下布局,如果您确定
newRide.setOnClickListener()被调用,那么我的第一个建议是将btnNewRide<Button>移动到末尾布局 XML,就在结束</RelativeLayout>标记之前。如果你想让它可点击,它应该在其他所有东西之上,z-order-wise。 -
@MikeM。 1 只是混乱,是什么让按钮布局没有触发。意味着为什么按钮应该在布局的最后一个任何特定的原因?
-
@W4R10CK 从布局膨胀的
Views 的z 顺序由它们添加到父级的顺序决定。在发布的布局中,首先列出的Button将位于“底部”;即,在垂直于屏幕的轴上更远。另外,在一个RelativeLayout,Views 可以重叠,覆盖其他Views。你可以看到ListView填充了RelativeLayout,所以很有可能它在触摸事件到达下面的Button之前拦截了它们。行为因版本而异的事实仅仅是由于事情从一个版本到另一个版本的变化。 -
@Mike M. 解释得很好!!!
标签: android onclicklistener android-button android-4.4-kitkat