将文本设置为网格视图中的特定文本视图
set text to a particular text view in grid view
我制作了一个资源文件 gallery_view.xml,其中有一个图像视图和一个位于图像视图之上的文本视图。这只是一个布局文件,我在网格视图上扩充此布局,我将在每个图像视图中显示来自 phone 图库的图像。
gallery_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:id="@+id/gvrl"
android:layout_width="112dp"
android:layout_height="120dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<androidx.cardview.widget.CardView
android:layout_width="110dp"
android:layout_height="108dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:cardCornerRadius="3dp">
<ImageView
android:id="@+id/gvivs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop">
</ImageView>
<TextView
android:id="@+id/check_text"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textAlignment="center"
android:onClick="text_view_clicked"
android:textColor="#FFFFFF"
android:background="@drawable/checkbox_unchecked"
android:textSize="12dp">
</TextView>
</androidx.cardview.widget.CardView>
</RelativeLayout>
</RelativeLayout>
当用户单击文本视图时,我只想在该特定文本视图上设置一些文本。为了知道在网格视图中单击了哪个图像视图,我们有 OnItemClickListener() 方法,我们可以从中获取它的位置,但我如何知道单击了哪个特定文本视图。
在我的 gallery_view.xml 文本视图中,我声明了
android:onClick="text_view_clicked"
而这个方法在我的Import_from_gallery.javaclass中是这样定义的
public void text_view_clicked(View v) {
Toast.makeText(Import_from_gallery.this, "Text View Clicked", Toast.LENGTH_SHORT).show();
}
Import_from_gallery.java 的布局文件是:
activity_import_from_gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Import_from_gallery">
<RelativeLayout
android:id="@+id/ifgab"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#0097A7">
<ImageButton
android:id="@+id/ifgib1"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:background="@drawable/ic_close_white_24dp">
</ImageButton>
<TextView
android:id="@+id/pselec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:maxWidth="100dp"
android:layout_marginLeft="60dp"
android:layout_toStartOf="@+id/ifgb1"
android:text="All Photos"
android:textColor="#FFFFFF"
android:textSize="18sp">
</TextView>
</RelativeLayout>
<GridView
android:id="@+id/gv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:numColumns="3">
</GridView>
</RelativeLayout>
现在我如何知道在众多文本视图中点击了哪个文本视图以及如何在该特定视图上设置文本。
谢谢
希望这对您有用 - 从网格项目(视图 v)中,您可以通过 ID 获得任何子视图。
gv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView selectedTextView = v.findViewById(R.id.pselec);
selectedTextView.setText("WHATEVER_VALUE");
}
});
我制作了一个资源文件 gallery_view.xml,其中有一个图像视图和一个位于图像视图之上的文本视图。这只是一个布局文件,我在网格视图上扩充此布局,我将在每个图像视图中显示来自 phone 图库的图像。
gallery_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:id="@+id/gvrl"
android:layout_width="112dp"
android:layout_height="120dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<androidx.cardview.widget.CardView
android:layout_width="110dp"
android:layout_height="108dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:cardCornerRadius="3dp">
<ImageView
android:id="@+id/gvivs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop">
</ImageView>
<TextView
android:id="@+id/check_text"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textAlignment="center"
android:onClick="text_view_clicked"
android:textColor="#FFFFFF"
android:background="@drawable/checkbox_unchecked"
android:textSize="12dp">
</TextView>
</androidx.cardview.widget.CardView>
</RelativeLayout>
</RelativeLayout>
当用户单击文本视图时,我只想在该特定文本视图上设置一些文本。为了知道在网格视图中单击了哪个图像视图,我们有 OnItemClickListener() 方法,我们可以从中获取它的位置,但我如何知道单击了哪个特定文本视图。
在我的 gallery_view.xml 文本视图中,我声明了
android:onClick="text_view_clicked"
而这个方法在我的Import_from_gallery.javaclass中是这样定义的
public void text_view_clicked(View v) {
Toast.makeText(Import_from_gallery.this, "Text View Clicked", Toast.LENGTH_SHORT).show();
}
Import_from_gallery.java 的布局文件是:
activity_import_from_gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Import_from_gallery">
<RelativeLayout
android:id="@+id/ifgab"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#0097A7">
<ImageButton
android:id="@+id/ifgib1"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:background="@drawable/ic_close_white_24dp">
</ImageButton>
<TextView
android:id="@+id/pselec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:maxWidth="100dp"
android:layout_marginLeft="60dp"
android:layout_toStartOf="@+id/ifgb1"
android:text="All Photos"
android:textColor="#FFFFFF"
android:textSize="18sp">
</TextView>
</RelativeLayout>
<GridView
android:id="@+id/gv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:numColumns="3">
</GridView>
</RelativeLayout>
现在我如何知道在众多文本视图中点击了哪个文本视图以及如何在该特定视图上设置文本。
谢谢
希望这对您有用 - 从网格项目(视图 v)中,您可以通过 ID 获得任何子视图。
gv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView selectedTextView = v.findViewById(R.id.pselec);
selectedTextView.setText("WHATEVER_VALUE");
}
});