【问题标题】:android why this button is not clickableandroid为什么这个按钮不可点击
【发布时间】:2013-02-12 19:29:24
【问题描述】:

我不知道为什么最后两个按钮不可点击,我多次使用背景(我为它们设置的)并且它适用于其他布局,我确信错误是类似 foucable 或 clickable 但是我不知道具体在哪里。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:fillViewport="true" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:orientation="vertical" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dip"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:layout_marginTop="20dip"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/b_orderMeal_selectRestaurant"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginRight="30dip"
                    android:text="@string/b_selectRestaurant" />

                <ImageView
                    android:id="@+id/iv_orderMeal_isSelectRestaurant"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_alignParentRight="true"
                    android:contentDescription="@string/iv_isSelected"
                    android:src="@drawable/x" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dip"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:layout_marginTop="20dip"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/b_orderMeal_selectMealItems"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginRight="30dip"
                    android:text="@string/b_select_MealItems" />

                <ImageView
                    android:id="@+id/iv_orderMeal_isSelectMealItems"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_alignParentRight="true"
                    android:contentDescription="@string/iv_isSelected"
                    android:src="@drawable/x" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dip"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:layout_marginTop="20dip"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/b_orderMeal_selectPayMethod"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginRight="30dip"
                    android:text="@string/b_selectPayMethod" />

                <ImageView
                    android:id="@+id/iv_orderMeal_isSelectPayMethod"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_alignParentRight="true"
                    android:contentDescription="@string/iv_isSelected"
                    android:src="@drawable/x" />
            </RelativeLayout>

            <Button
                android:id="@+id/b_orderMeal_checkBasket"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="40dip"
                android:layout_marginTop="20dip"
                android:layout_weight="1"
                android:text="@string/b_check_basket" />

            <TextView
                android:id="@+id/tv_orderMeal_errorMessage"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dip"
                android:layout_marginRight="20dip"
                android:layout_marginTop="30dip"
                android:layout_weight="1"
                android:text="@string/tv_orderMeal_errorMessage"
                android:textColor="#FF0000"
                android:textSize="20dip"
                android:textStyle="bold"
                android:typeface="sans" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/bg_selector"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/b_orderMeal_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="4dip"
                android:layout_weight="1"
                android:background="@drawable/button_bg"
                android:text="@string/b_send"
                android:focusable="true"
                android:clickable="true"
                android:textColor="#FFFFFF" />

            <Button
                android:id="@+id/b_orderMeal_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dip"
                android:layout_weight="1"
                android:background="@drawable/button_bg"
                android:text="@string/b_cancel"
                android:clickable="true"
                android:textColor="#FFFFFF" />
        </LinearLayout>
    </RelativeLayout>

</ScrollView>

我说的是最后两个按钮

java

b_send = (Button) findViewById(R.id.b_orderMeal_send);
        b_send.setOnClickListener(this);
        b_cancel = (Button) findViewById(R.id.b_orderMeal_cancel);
        b_cancel.setOnClickListener(this);

点击

@Override
    public void onClick(View v) {
      case R.id.b_orderMeal_cancel:
        Toast.makeText(this, "roma", Toast.LENGTH_LONG);
        break;          
    }

注意

吐司没有出现

【问题讨论】:

  • 你怎么知道它们不可点击?你的java代码在哪里?
  • 希望您为最后两个按钮添加了 OnClickListener 的代码。请检查您的代码并确认
  • 你应该在onClick中添加一些日志或东西,否则你怎么知道它是否可点击?
  • 请发布 onClick 的整个实现。你在 switch 语句中使用了什么?

标签: android android-layout android-xml android-button


【解决方案1】:

我不知道为什么最后两个按钮不可点击,我使用了 背景(我设置给他们)很多次,它与另一个 布局,我确信错误是 foucable 或 可点击,但我不知道具体在哪里。

我认为您的XML 设计在各方面都还可以,所以问题很可能出现在Java。您很可能忘记为您的Button(s) 注册OnClickListener

Button btn = (Button) findViewById(R.id.b_orderMeal_send);
btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
       Toast.makeText(this, "Click event works.", Toast.LENGTH_SHORT).show();
     }
 }));

或者你可以使用OnClickListener接口然后

public void onClick(View v) {
   switch (v.getId()) {
      case R.id.b_orderMeal_send:
         Toast.makeText(this, "Click event works.", Toast.LENGTH_SHORT).show();
      break;
   }
}

【讨论】:

  • 我的活动是实现 onclicklistenr
  • 我不知道问题出在哪里,我对这个问题感到困惑
  • 检查我更新的答案。我建议将 int 值与 switch than equals 方法进行比较...
  • 我已经在使用 switch case 但抱歉我没有告诉你,对不起,它仍然无法正常工作,我已经完成了你所有的代码,
  • 它必须有效,因为它对我有效。复制我的代码并清理并重建您的项目,然后再试一次。
【解决方案2】:

有时模拟器会造成问题,所以我想建议你们使用真正的 Android 设备来检查/运行您的应用程序

【讨论】:

    【解决方案3】:

    用下面的方法替换你的onClick() 方法,让我们知道 logcat。 您似乎正在尝试显示 toast,但缺少显示 toast 的 .show() 方法。 试试下面的代码,让我们知道

     @Override
     public void onClick(View v) {
     if(v.equals(b_send)){
         Toast.makeText(this,"b_send clicked", Toast.LENGTH_LONG).show();
         Log.d("TEMP TAG","b_send clicked");
     } else if(v.equals(b_cancel)){ 
        Toast.makeText(this,"b_cancel clicked", Toast.LENGTH_LONG).show();
          Log.d("TEMP TAG","b_cancel clicked");
          }
    
      }
    

    【讨论】:

    • 何为虔诚?我总是做开关盒,哪里错了?无论如何我都会尝试你的解决方案
    • 再检查一遍,你在toast.makeText方法上缺少.show(),在你的activity中实现View.onClickListener接口。
    • 你可以使用 switch 或 equal,结果会一样。所以不用担心。
    • 但我不建议为此目标使用 equals 方法。它很脏,有更好更干净的解决方案。
    • @user2059935 欢迎您,如果是 .show() 问题,您应该将我的答案标记为已接受,因为我首先明确提到,不是吗? nyways 快乐编码 :)
    【解决方案4】:

    在您的按钮的 xml 中设置 clickable = true 并在您的代码中尝试此操作。

    findViewById(R.id.b_orderMeal_send).setOnClickListener(commonClickListener);
    findViewById(R.id.b_orderMeal_cancel).setOnClickListener(commonClickListener);
    
    
    private OnClickListener commonClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        int selectedItemId = v.getId();
        switch (selectedItemId) {
        case R.id.b_orderMeal_send:
            Toast.makeText(this, "roma", Toast.LENGTH_LONG).show();
            break;
        case R.id.b_orderMeal_cancel:
        Toast.makeText(this, "roma", Toast.LENGTH_LONG).show();
            break;
    
    
        }
    }
    

    【讨论】:

      【解决方案5】:

      同样的事情不断发生在我身上,但我的情况有点不同。首先,按钮工作正常,两个都可以点击,然后在几次重新启动后突然完全停止工作。

      我只是 Quit Emulator 然后重新启动它,按钮又开始工作了

      【讨论】:

        猜你喜欢
        • 2020-06-12
        • 2021-12-05
        • 1970-01-01
        • 2019-08-19
        • 2017-10-30
        • 2015-09-26
        • 2013-11-07
        • 2017-10-12
        • 2017-07-15
        相关资源
        最近更新 更多