如何更改列表视图中文本视图的可见性
How to change visibility of a textview which is in a listview
我的 activity_main.xml 中有一个 listView。我为列表视图的行使用了布局(list_layout)。 list_layout 包含 3 个 textView。我在 Mainactivity 中添加了一个名为 "Setting" 的 activity。我想用按钮更改 list_layout 的 3.textView 的可见性。settin.java。
我的意思是当我点击按钮时(按钮代码进入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; ...
//Setting.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);
}
}
activity_setting.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY BUTTON"
android:onClick="click"/>
给按钮添加点击监听器有不同的方法,参考这个link:
add onclick listener to predefined button?
在您的情况下,您可以在 activity 接口 OnClickListener
中实现
public class Setting extends AppCompatActivity implements OnClickListener
那么你应该将你的方法重命名为 onClick
并且在 activity 的 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
我的 activity_main.xml 中有一个 listView。我为列表视图的行使用了布局(list_layout)。 list_layout 包含 3 个 textView。我在 Mainactivity 中添加了一个名为 "Setting" 的 activity。我想用按钮更改 list_layout 的 3.textView 的可见性。settin.java。
我的意思是当我点击按钮时(按钮代码进入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; ...
//Setting.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);
}
}
activity_setting.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY BUTTON"
android:onClick="click"/>
给按钮添加点击监听器有不同的方法,参考这个link: add onclick listener to predefined button?
在您的情况下,您可以在 activity 接口 OnClickListener
中实现public class Setting extends AppCompatActivity implements OnClickListener
那么你应该将你的方法重命名为 onClick
并且在 activity 的 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