2016-05-10 46 views
-3

這是我的XML代碼,我已經2個按鈕向上和向下箭頭,我想以顯示如下內容時,向下箭頭被點擊了,否則我想如何使文本可見,匆匆上按鈕單擊事件

lin1.setVisibility(View.VISIBLE); 

在我的代碼bt它不工作..可以有人幫我這個...?

在此先感謝

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/Hrmbg" 
    tools:context="com.agna.hrm_sainmarks.PersonalDetails" > 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      > 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginTop="10dp" 
       android:id="@+id/lin1" 
       android:background="@color/Hrmbg" 
       android:weightSum="2" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:layout_gravity="right" 
        android:text="@string/tpersonal" 
        android:textSize="18dp" /> 

       <ImageButton 
        android:id="@+id/up" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.50" 
        android:background="@null" 
        android:src="@drawable/uparrow" 
        android:visibility="visible" /> 

       <ImageButton 
        android:id="@+id/down" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.50" 
        android:background="@null" 
        android:src="@drawable/downarrow" /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/marital" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/marital" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 
        android:paddingRight="10dp" 
        android:text="Married" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View01" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/spouse" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/spouse" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 
       android:paddingRight="10dp" 
        android:text="Shalini" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View02" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/spouseprofession" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/spouseprofession" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 
        android:paddingRight="10dp" 
        android:text="House Wife" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View03" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/children" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/children" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 
        android:paddingRight="10dp" 
        android:text="Two" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View04" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/fathername" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/fathername" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 
        android:paddingRight="10dp" 
        android:text="Aravind" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View05" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/fatherprofession" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/fatherprofession" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 
         android:paddingRight="10dp" 

        android:text="Business" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View06" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/fatherage" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/fatherage" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 
         android:paddingRight="10dp" 
        android:text="61" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View07" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/mothername" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/mothername" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 

         android:paddingRight="10dp" 
        android:text="Meenakshi" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View08" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/motherprofession" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/motherprofession" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 
         android:paddingRight="10dp" 

        android:text="House Wife" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View09" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/motherage" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/motherage" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 

        android:paddingRight="10dp" 
        android:text="59" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View10" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginTop="10dp" 
       android:background="@color/Hrmbg" 
       android:weightSum="2" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:layout_gravity="right" 
        android:text="@string/sibling" 
        android:textSize="18dp" /> 

       <ImageButton 
        android:id="@+id/othrdown" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.50" 
        android:background="@null" 
        android:src="@drawable/uparrow" 
        android:visibility="visible" /> 

       <ImageButton 
        android:id="@+id/othrup" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.50" 
        android:background="@null" 
        android:src="@drawable/downarrow" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View11" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/name" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/name" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 

        android:paddingRight="10dp" 
        android:text="Ravi Varma" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View12" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/age" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/age" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 
         android:paddingRight="10dp" 

        android:text="25" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View13" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/profession" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/profession" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 

        android:paddingRight="10dp" 
        android:text="Professor" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 

      <View 
       android:id="@+id/View14" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/Hrmbg" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/White" 
       android:paddingBottom="10dp" 
       android:paddingTop="10dp" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:text="@string/company" 
        android:textSize="18dp" /> 

       <TextView 
        android:id="@+id/company" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:gravity="right" 

        android:paddingRight="10dp" 
        android:text="PSG CAS" 
        android:textSize="18dp" 
        android:textStyle="normal" /> 
      </LinearLayout> 
     </LinearLayout> 
    </ScrollView> 

</RelativeLayout> 

PersonalDetails.java

public class PersonalDetails extends Activity implements OnClickListener { 

     ImageButton up,down; 
     LinearLayout lin1; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_personal_details); 
      up = (ImageButton)findViewById(R.id.up); 
         down = (ImageButton)findViewById(R.id.down); 
lin1=(LinearLayout)findViewById(R.id.lin1); 
      } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 

      getMenuInflater().inflate(R.menu.personal_details, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 

      int id = item.getItemId(); 
      if (id == R.id.action_settings) { 
       return true; 
      } 
      return super.onOptionsItemSelected(item); 
     } 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(v.equals(up)) 
      { 
       lin1.setVisibility(View.VISIBLE); 
      } 
      if(v.equals(down)) 
      { 

       down.setVisibility(View.INVISIBLE); 
      } 
     } 
    } 
+0

哪裏是你的MainActivity.java代碼? –

+0

讓點擊偵聽器在您點擊箭頭後觸發?什麼是lin1? – panda

回答

1

使其可見

YourSelectedItem.setVisibility(View.VISIBLE); 

爲了讓隱形

YourSelectedItem.setVisibility (View.GONE); 

編輯

你的情況

@Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(v.equals(up)) 
      { 
       lin1.setVisibility(View.VISIBLE); 
      } 
      if(v.equals(down)) 
      { 

       down.setVisibility(View.GONE); 
      } 
     } 
1

使用此代碼,它可能會幫你。 在xml中創建兩個Button。

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button_up"/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button_down"/> 

在活動代碼:

Button button_up = (Button)findViewById(R.id.button_up); 
button_up.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) 
    { 
     lin1.setVisibility(View.VISIBLE); 
    } 
}); 

Button button_down = (Button)findViewById(R.id.button_down); 
button_down.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) 
     { 
      lin1.setVisibility(View.GONE); 
     } 
    }); 
+0

其實我想要下面的lin1數據是隱形的,當我點擊箭頭時,可見當我點擊箭頭 –

+0

@PreethiRajan這是工作只是試圖設置你的佈局,並將'ImageButton'改爲'Button'。 –

+0

@PreethiRajan在'lin1'中有很多'LinearLayout',你只需要'VISIBLE'和'GONE'。 –

0

你失蹤了按鈕setOnClickListener,編輯代碼這樣

up.setOnClickListener(this); 
down.setOnClickListener(this); 

而且

@Override 
public void onClick(View v) { 
    switch (v.getId()){ 
    case R.id.up: 
     lin1.setVisibility(View.VISIBLE); 
     break; 

    case R.id.down: 
     down.setVisibility(View.INVISIBLE); 
     break; 
    } 
} 
0

使兩個按鈕以XML作爲:

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/up"/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/down" 
    android:visibility="gone"/> 

然後在活動中,您可以使用該按鈕爲:

Button up = (Button)findViewById(R.id.up); 
Button down = (Button)findViewById(R.id.down); 
up.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) 
    { 
     lin1.setVisibility(View.GONE); 
     down.setVisibility(View.VISIBLE) 
    } 
}); 


down.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) 
    { 
     lin1.setVisibility(View.VISIBLE); 
     downsetVisibility(View.GONE); 
    } 
});