2016-04-26 52 views
0

我有一个ImageButton和一个相同大小的宽度和高度的相对视图内的TextView。默认情况下,TextView被禁用,ImageButton被启用。但是,在这种情况下,我无法点击ImageButton。对彼此的两个意见,但只显示一个?

当我启用TextView并禁用ImageButton时,我可以点击TextView。我认为这是因为它在XML中的ImageButton下。

有没有人知道我怎么能得到这种“切换”功能,其中两个项目的大小相同,但是其中只有一个是可点击的。

  <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="15dp"> 

       <ImageButton 
        android:id="@+id/pp_note_1" 
        android:layout_width="150dp" 
        android:layout_height="50dp" 
        android:layout_alignParentRight="true" 
        android:background="@android:color/white"/> 

       <AutoCompleteTextView 
        android:enabled="false" 
        android:id="@+id/pp_autonote_1" 
        android:layout_alignParentRight="true" 
        android:layout_width="150dp" 
        android:layout_height="50dp" /> 


      </RelativeLayout> 
+0

你可以让你的生活更轻松,并使用ViewSwitcher。或者不要禁用尝试将可见性设置为通过。 – ElDuderino

+0

@ElDuderino他从来没有听说过ViewSwitcher,我可以看看这个。另外,如果更改可见性,我可以使用背景吗? – Brejuro

+0

如果将ImageButton的可见性设置为GONE,ImageButton的背景将不会再显示。如果您需要两个视图的背景,请将其设置为RelativeLayout或ViewSwitcher,然后再使用它。您当然也可以将它设置为TextView,这样两个视图都有背景。 – ElDuderino

回答

0

看看到

public void setFocusable (boolean focusable) 
public void setFocusableInTouchMode (boolean focusableInTouchMode) 
public void setClickable (boolean clickable) 

View

0

这是交换以用ViewSwitcher两个视图工作的例子:

viewswitcher.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/content" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ViewSwitcher 
    android:id="@+id/viewSwitcher" 
    android:layout_centerInParent="true" 
    android:layout_width="match_parent" 
    android:layout_height="96dp"> 

    <ImageButton 
     android:id="@+id/image" 
     android:src="@mipmap/ic_launcher" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <TextView 
     android:id="@+id/text" 
     android:text="Text" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</ViewSwitcher> 

</RelativeLayout> 

活动的onCreate()

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.viewswitcher); 

    final ViewSwitcher viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher); 
    final ImageButton imageButton = (ImageButton) findViewById(R.id.image); 
    imageButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      viewSwitcher.showNext(); 
     } 
    }); 
} 

以设置过渡动画,使用viewPager.setInAnimation()和.setOutAnimation()。

+0

我意识到一个ViewSwitcher不适合我,因为这个切换是一个设置,所以当应用程序再次启动时,我检查已保存的首选项并启用/禁用一个,这是ViewSwitcher无法完成的(仅showNext和showPrevious)。我仍然无法弄清楚如何使这项工作。 – Brejuro

+0

那么,而不是启用/禁用,只做viewPager.showNext()或不这样做。这样你可以显示ImageButton或TextView。您可以使用viewPager.reset()来重置所有内容。所以你可以从零开始。 – ElDuderino

+0

我不明白为什么启用和禁用不会工作。它仍然不会让我使用任何一个更高的XML。 – Brejuro