【问题标题】:android how to make relativelayout not clickable and its itme clickableandroid如何使relativelayout不可点击并且它的itme可点击
【发布时间】:2013-01-24 18:08:11
【问题描述】:

我有一个带有 relativelayout 标题的列表视图,当我点击 relativelayout 中的任何空间时,它会变成白色,我不想要那个白色,如果用户点击空间,什么都不会发生,但是如果他点击将图像放入该相对布局中,我可以在 onclick 中处理它。

这是相对布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rl_simple_list_item_header"
    android:focusable="true"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/orderMeal"
        android:layout_marginLeft="20dip"
        android:id="@+id/tv_restaurant_description_orderMeal"
        android:drawableTop="@drawable/order_meal" />
    <TextView 
        android:id="@+id/iv_simple_list_item_header_favorite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/favorite"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dip"
        android:drawableTop="@drawable/favorite"/>
</RelativeLayout>

编辑

01-25 21:00:42.718: E/AndroidRuntime(24121): FATAL EXCEPTION: main
01-25 21:00:42.718: E/AndroidRuntime(24121): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Syriatel.EatTel/com.Syriatel.EatTel.Restaurant_Description}: java.lang.NullPointerException
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.os.Looper.loop(Looper.java:130)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.main(ActivityThread.java:3701)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at java.lang.reflect.Method.invokeNative(Native Method)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at java.lang.reflect.Method.invoke(Method.java:507)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at dalvik.system.NativeStart.main(Native Method)
01-25 21:00:42.718: E/AndroidRuntime(24121): Caused by: java.lang.NullPointerException
01-25 21:00:42.718: E/AndroidRuntime(24121):    at com.Syriatel.EatTel.Restaurant_Description.initialize(Restaurant_Description.java:46)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at com.Syriatel.EatTel.Restaurant_Description.onCreate(Restaurant_Description.java:35)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-25 21:00:42.718: E/AndroidRuntime(24121):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
01-25 21:00:42.718: E/AndroidRuntime(24121):    ... 11 more

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    设置布局可点击,不带任何onClickListener

    <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/info1"
                android:clickable="true">
    

    【讨论】:

      【解决方案2】:

      尝试让您的RelativeLayout 具有焦点:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:id="@+id/rl_simple_list_item_header"
          android:orientation="horizontal"
          android:focusable="true"
          android:focusableInTouchMode="true" >
      

      更新

      如果你想在标题中点击TextView,你可以这样做:

      View header = LayoutInflater.from(this).inflate(R.layout.simple_list_item_header, null);
      View favorite = header.findViewById(R.id.iv_simple_list_item_header_favorite);
      
      lv_restaurantInformation = (ListView) findViewById(R.id.lv_restaurant_description_information);
      lv_restaurantInformation.addHeaderView(header);
      
      favorite.setOnClickListener(this);
      

      然后在ActivityonClick方法中处理:

      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
          switch (v.getId()) {
          case R.id.iv_simple_list_item_header_favorite:
              Toast.makeText(this, "onClick", Toast.LENGTH_SHORT).show();
              break;
      
          default:
              break;
          }
      }
      

      【讨论】:

      • 你知道谁可以从活动的列表视图标题中获取元素吗?
      • 请检查我的编辑,我添加了代码,你能告诉我如何在我的活动中获取图像视图吗?
      • 我在布局中没有看到任何ImageView
      • 没关系,现在你的代码使布局和它的元素是可点击的,我希望文本视图仍然可以点击
      • tv_favorite = (TextView)findViewById(R.id.iv_simple_list_item_header_favorite); @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.iv_simple_list_item_header_favorite: tv_favorite.setText("TTTTTTTT"); break; default: break; } }
      【解决方案3】:

      如果您将 onclicklistener 分配给实际项目,而不是在活动中实现它,这应该不是问题。

      tv_favorite = (TextView) header.findViewById(R.id.iv_simple_list_item_header_favorite);
      tv_favorite.setOnClickListener( new OnClickListener(){
          @Override
      public void onClick(View v) {
              tv_favorite.setText("asdfasdfasdf");
      }
      
      });
      

      如果您想要 ListView 中的单个项目(而不是标题),您应该使用为整个 ListView 设置的 OnItemClickListener。

      【讨论】:

      • 没有 onitemclicklisterner 因为我使用的是活动类而不是 listactivity
      • 不,您可以执行 `lv_restaurantInformation.setOnItemClickListener(...)' 只会获得列表中项目的点击次数,我认为这是您实际尝试执行的操作 您的问题有点令人困惑,你能制作一个图表并显示你真正想要可点击的部分吗?
      • 我把这个 `tv_favorite = (TextView) header .findViewById(R.id.iv_simple_list_item_header_favorite); lv_restaurantInformation.addHeaderView(header); lv_restaurantInformation.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3) { // TODO 自动生成的方法存根 if(arg3 == R.id.iv_simple_list_item_header_favorite) tv_favorite.setText("asdfasdfasdf"); } }); ` 但什么也没发生,是不是标题不可点击?
      • 我不知道什么是好的andwer,我自己试试,非常感谢
      猜你喜欢
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 2019-08-04
      • 2015-07-17
      • 2013-08-07
      • 1970-01-01
      相关资源
      最近更新 更多