gpt4 book ai didi

android - 如何展开和折叠以 EditText 作为其子项的 textView

转载 作者:行者123 更新时间:2023-11-29 20:19:39 27 4
gpt4 key购买 nike

我是 android 新手..在我的 xml 文件中,我使用了三个 TextView ,在里面我为每个 TextView 使用了三个 edittext ..我想要的是当我按下一个特定的 TextView 时,它必须展开才能得到它们child(edittexts)..当我再次按下时,textview 应该会折叠起来。

现在在我的代码中,当我按下 textview 时它会展开,但是当我再次按下时它不会折叠。

下面是我的代码..任何帮助将不胜感激...提前致谢..

Java文件

分类页面.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CategoryPage extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category_page);

TextView btnProfile = (TextView) findViewById(R.id.btnprofile);
TextView btnSettings = (TextView) findViewById(R.id.btncitizen);
TextView btnPrivacy = (TextView) findViewById(R.id.btnprivacy);

View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);

View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);

View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);

btnProfile.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.VISIBLE);

View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);

View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);

}
});

btnSettings.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);

View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.VISIBLE);

View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);

}
});

btnPrivacy.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);

View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);

View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.VISIBLE);

}
});
}
}

XML文件

activity_category_page.xml

<?xml version="1.0" encoding="utf-8"?>
<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="#fff">

<LinearLayout android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="5dip"
android:paddingBottom="5dip">

<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:src="@drawable/miiskylogo" />
</LinearLayout>

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/header" >

<LinearLayout
android:id="@+id/root_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFFFF"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:orientation="vertical">

<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:text="@string/Text"
android:textSize="20dp"
android:textStyle="bold"/>

<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview1"
android:text="@string/Text2"
android:layout_marginBottom="10dp"
android:textSize="13dp"
/>

<TextView
android:id="@+id/btnprofile"
android:layout_width="match_parent"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:background="@drawable/verifyedit"
android:text="Profile Vault"
android:textColor="#000"
android:textSize="15dp" />

<LinearLayout
android:id="@+id/panelProfile"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">

<LinearLayout
android:id="@+id/panelProfile1"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="17dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >

<EditText
android:id="@+id/salutation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="@drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Salutation" >

<requestFocus />
</EditText>
</LinearLayout>

<LinearLayout
android:id="@+id/panelProfile2"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >

<EditText
android:id="@+id/firstname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="@drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="First Name" />

</LinearLayout>

<LinearLayout
android:id="@+id/panelProfile3"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >

<EditText
android:id="@+id/middlename"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="@drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Middle Name" />

</LinearLayout>

<LinearLayout
android:id="@+id/panelProfile4"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >

<EditText
android:id="@+id/lastname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="@drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Last Name" />

</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/btncitizen"
android:layout_width="match_parent"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_marginTop="15dp"
android:background="@drawable/verifyedit"
android:text="Citizenship/Registration"
android:textColor="#000"
android:textSize="15dp" />

<LinearLayout
android:id="@+id/panelSettings"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">

<LinearLayout
android:id="@+id/panelSettings1"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="17dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >

<EditText
android:id="@+id/currentaddress"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="@drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Current address" >

<requestFocus />
</EditText>
</LinearLayout>

<LinearLayout
android:id="@+id/panelSettings2"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >

<EditText
android:id="@+id/houseno"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="@drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="House no" />

</LinearLayout>

<LinearLayout
android:id="@+id/panelSettings3"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >

<EditText
android:id="@+id/roadno"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="@drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Road no" />

</LinearLayout>

<LinearLayout
android:id="@+id/panelSettings4"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >

<EditText
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="@drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Location" />

</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/btnprivacy"
android:layout_width="match_parent"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_marginTop="15dp"
android:background="@drawable/verifyedit"
android:text="Banking"
android:textColor="#000"
android:textSize="15dp" />

<LinearLayout
android:id="@+id/panelPrivacy"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">

<LinearLayout
android:id="@+id/panelPrivacy1"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="17dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >

<EditText
android:id="@+id/bankacc"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="@drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Bank account no" >
<requestFocus />
</EditText>
</LinearLayout>

<LinearLayout
android:id="@+id/panelPrivacy2"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >

<EditText
android:id="@+id/acctype"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="@drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Account Type" />

</LinearLayout>

<LinearLayout
android:id="@+id/panelPrivacy3"
android:layout_width="280dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
android:orientation="horizontal" >

<EditText
android:id="@+id/bankcredit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.81"
android:background="@drawable/verifyedit"
android:ems="10"
android:paddingLeft="10dp"
android:hint="Bank Credit Card No" />

</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>

最佳答案

检查 View 是否可见 onClick 的 TextView:

if(panelProfile.getVisibility() == View.VISIBLE) {
panelProfile.setVisiblity(View.GONE);
} else {
panelProfile.setVisiblity(View.VISIBLE);
}

注意:您要多次声明下面的代码,而不是只声明一次。

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

同上,

View panelSettings = findViewById(R.id.panelSettings);
View panelPrivacy = findViewById(R.id.panelPrivacy);

关于android - 如何展开和折叠以 EditText 作为其子项的 textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33413785/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com