android - 如何更改列表视图中的文本视图的可见性
问题描述
我的 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"/>
解决方案
向按钮添加点击侦听器有多种方法,请参阅此链接: 将 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
推荐阅读
- typo3 - TYPO3 8.7.13 - getArguments() 返回无响应
- azure - Azure 数据目录:资产和用户信息框不见了,如何添加回来?
- vim - 按文件过滤 vim 的 quickfix 窗口
- mysql - 使用 Alteryx 将表从一个数据库复制到另一个数据库
- r - 在 phytools 中使用 plotBranchbyTrait() 时,'use.edge.length = FALSE' 似乎不起作用
- node.js - 如何抛出我自己的不显示调用堆栈的错误?
- json - 即使返回的 var 不为空,Web API 控制器也会返回空 JSON
- tmux - 命令完成时停止 tmux 分离会话关闭
- build - 有没有一种快速的方法可以在 Qt Creator 中构建所有目标?
- iterator - 在 Rust 中生成排序地图