我的activity_main.xml中有一个listView。我为 ListView 的行使用了布局(list_layout)。 list_layout包含3个textView。我在我的 Mainactivity 中添加了一个名为“Setting”的 Activity 。我想用按钮更改 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; ...
//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"/>
最佳答案
为按钮添加点击监听器有多种方法,请引用此链接: add onclick listener to predefined button?
在您的情况下,您可以在您的 Activity 中实现 OnClickListener 接口(interface)
public class Setting extends AppCompatActivity implements OnClickListener
那么你应该将你的方法 click 重命名为 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
关于android - 如何更改 ListView 中 TextView 的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55631668/