首页 > 解决方案 > 如何更改列表视图中的文本视图的可见性

问题描述

我的 activity_main.xml 中有一个 listView 。我为列表视图的行使用了布局(list_layout)。list_layout 包含 3 个 textView。我在我的 Mainactivity 中添加了一个名为“Setting”的活动。我想用一个按钮从 settin.java 更改 list_layout 的 3. textView 的可见性。

我的意思是当我单击按钮时(按钮代码进入setting.java(按钮进入activity_setting.xml))list_layout 的3.textview 必须不可见。

这是来自activity_main.xml

    <ListView
        android:id="@+id/listem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ListView>

这是 list_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
   <TextView
.../>
   <TextView
.../>
   <TextView
        android:id="@+id/turkish_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:visibility="visible"/>

</LinearLayout>

MainActivity.Java

     ...    listview = (ListView) findViewById(R.id.listem);
DataHelper.Database data = new DataHelper.Database(MainActivity.this);
            ArrayList<HashMap<String, String>> Liste = data.Listele();
            ListAdapter adapter = new SimpleAdapter(MainActivity.this, Liste, R.layout.list_layout, new String[]{"id", "title", "subtitle"}, new int[]{R.id.kelime_id, R.id.english_id, R.id.turkish_id});
            listview.setAdapter(adapter);
      ...
     public boolean onOptionsItemSelected(MenuItem item) {
             switch (item.getItemId()) {

                case R.id.settings:
                    Intent intent = new Intent(MainActivity.this, Setting.class);
                    startActivity(intent);
                    break;  ...

//设置.Java

public class Setting extends AppCompatActivity {

    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
    }

    public void click(View view) {//<-----Here is my button's code
        textView=(TextView)view.findViewById(R.id.turkish_id);
        textView.setVisibility(View.INVISIBLE);
    }
}

活动设置.xml

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="MY BUTTON"
    android:onClick="click"/>

标签: androidtextviewvisibility

解决方案


向按钮添加点击侦听器有多种方法,请参阅此链接: 将 onclick 侦听器添加到预定义按钮?

在您的情况下,您可以在活动中实现接口 OnClickListener

public class Setting extends AppCompatActivity implements OnClickListener

那么你应该将你的方法重命名为 onClick

在您的活动的 onCreate 中,您应该添加该行

findViewById(R.id.yourIdButton).setOnClickListener(this);

不要忘记给你的按钮一个 id

<Button
    android:"@+id/yourIdButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="MY BUTTON"
    android:onClick="click"/>

您还可以使用“View.GONE”代替“View.INVISIBLE”从布局中完全删除 TextView


推荐阅读