2015-03-13 34 views
0

這裏對編程非常新穎。我正在嘗試在.xml和.java中爲Android編程一個活動,並試圖找出是否可以通過按下按鈕來切換一個元素。用於在ANDROID中切換元素的XML或JAVA代碼

我正在使用的屏幕將有一個選項來搜索項目列表以及將項目添加到此列表的選項。我希望搜索欄和任何添加選項默認隱藏,並可以通過按下相應按鈕來顯示其中任何一個。按搜索選項按鈕,應出現搜索欄,按添加選項按鈕,搜索欄應隱藏,並在其位置應顯示添加選項。

這是可能的.xml或.java,如果是的話,我應該尋找什麼樣的元素或方法來實現這一點?下面你會發現我在.xml中的代碼,其中editText1應該是搜索欄,如果按下imageButton2,應該用添加選項替換它。

非常感謝提前。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bgmenu" 
    android:orientation="vertical" > 

    <ImageButton 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="#FFFFFF" 
     android:scaleType="fitStart" 
     android:src="@drawable/bproductsbanner" 
     android:paddingStart="5dp" 
     android:paddingTop="15dp" 
     android:id="@+id/ibProductsBanner"/> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/bgminibutton" > 

     <ImageButton 
      android:id="@+id/imageButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginEnd="10dp" 
      android:background="#00000000" 
      android:src="@drawable/bssearch" /> 

     <ImageButton 
      android:id="@+id/imageButton2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_marginEnd="10dp" 
      android:layout_toStartOf="@+id/imageButton1" 
      android:background="#00000000" 
      android:src="@drawable/bsadd" /> 
    </RelativeLayout> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="Search.." /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Product List" 
     android:layout_marginTop="20dp" 
     android:layout_marginStart="5dp" 
     android:gravity="center"/> 

</LinearLayout> 
+0

請告訴我們您到目前爲止嘗試過的方法。 – 2015-03-13 14:38:31

回答

0

你必須設置能見度,走在你的XML到你的搜索欄:

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:hint="Search.." 
    android:visibility="gone"/> 

然後你必須改變的知名度,當你點擊按鈕:

ImageButton ib = (ImageButton) findViewById(R.id.imageButton1); 
final EditText search = (EditText) findViewById(R.id.editText1); 

ib.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     search.setVisibility(View.VISIBLE); 
    } 
}); 

附:我假設您的搜索欄是EditText,編號爲editText1,您的選項按鈕是ImageButton,編號爲imageButton1

+0

正確!看起來像setVisibility行是我正在尋找。非常感謝! – 2015-03-13 15:20:04

+0

@LarsScholing如果它解決了你的問題,接受或upvote我們的兩個答案之一! – 2015-03-13 15:22:41

+0

嘗試這樣做,但它似乎需要15的最低聲望upvote。目前只有1個聲望,因爲我只註冊了Stackoverflow 編輯:啊接受答案似乎工作;) – 2015-03-13 15:26:29

1

假設你已經在你的XML作爲一個EditText

<EditText android:id="@+id/search" ... 

然後在你的活動(JAVA)定義的搜索欄,你做

View search = findViewById(R.id.search); 

得到它的引用,那麼當點擊相應的按鈕時,您可以使用可見性屬性進行遊戲

search.setVisibility(View.VISIBLE); // View.GONE 
+0

這聽起來像它應該工作。我嘗試在我的java代碼中實現可見性線,似乎應該可以將元素放在RelativeLayout中的eachother之上。非常感謝! – 2015-03-13 15:14:41