【问题标题】:Button does not trigger onClick method按钮不触发 onClick 方法
【发布时间】: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 &lt;Button&gt; 移动到末尾布局 XML,就在结束 &lt;/RelativeLayout&gt; 标记之前。如果你想让它可点击,它应该在其他所有东西之上,z-order-wise。
  • @MikeM。 1 只是混乱,是什么让按钮布局没有触发。意味着为什么按钮应该在布局的最后一个任何特定的原因?
  • @W4R10CK 从布局膨胀的Views 的z 顺序由它们添加到父级的顺序决定。在发布的布局中,首先列出的Button 将位于“底部”;即,在垂直于屏幕的轴上更远。另外,在一个RelativeLayoutViews 可以重叠,覆盖其他Views。你可以看到ListView 填充了RelativeLayout,所以很有可能它在触摸事件到达下面的Button 之前拦截了它们。行为因版本而异的事实仅仅是由于事情从一个版本到另一个版本的变化。
  • @Mike M. 解释得很好!!!

标签: android onclicklistener android-button android-4.4-kitkat


【解决方案1】:
 if (!(Utilities.getStringFromPrefs(getApplicationContext(), "userType")
    .equals("driver")))

我认为问题出在这句话上。这个条件不成立

【讨论】:

  • 这不是问题,我正在清楚地登录那个..完美
  • 您能发布您的布局吗...您的按钮可能被其他视图隐藏了。
  • 在此处发送 xml 布局
  • 我的代码在所有版本的设备中都能正常工作,但在 kitkat 版本中,按钮 onclick 不起作用
  • 否则,为什么要添加此代码 newRide = (Button) findViewById(R.id.btnNewRide); ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-03
  • 2022-08-18
  • 1970-01-01
  • 2012-07-12
相关资源
最近更新 更多