如何使文本可见并继续按下单击事件

时间:2016-05-10 04:19:51

标签: android visibility

这是我的Xml代码,其中我有2个按钮向上箭头和向下箭头我想在点击向下箭头时显示在内容下面,否则我试过

lin1.setVisibility(View.VISIBLE);

在我的代码中它不起作用..有人可以帮我这个......?

提前致谢

    <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);
            }
        }
    }

4 个答案:

答案 0 :(得分: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 :(得分: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);
        } 
    });

答案 2 :(得分:0)

你缺少setOnClickListener for button,像这样编辑你的代码

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;
    }
}  

答案 3 :(得分: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);
    } 
});
相关问题