Android代码:当点击图像时,吐司应该通过其图像名称显示。

时间:2016-08-11 09:20:30

标签: android android-fragments android-studio-2.1 cardview

我创建了一张卡片视图。现在,我想显示带有单击的图像名称的A TOAST消息。请帮忙......

我的JAVA代码(片段)添加了图像..

public void prepareAlbums(){
        int[] covers = new int[]{
                R.drawable.album1,
                R.drawable.album2,
                R.drawable.album3,
                R.drawable.album4,
                R.drawable.album5,
                R.drawable.album6,
                R.drawable.album7,
                R.drawable.album8,
                R.drawable.album9,
                R.drawable.album10,
                R.drawable.album11};
        int j=0;

        for(int i=0;i<=10;i++){


            Album  a=new Album("Song"+i,i,covers[j]);
            j++;
            albumList.add(a);
        }
        adapter.notifyDataSetChanged();
    }

MY XML CODE ..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="@dimen/card_margin"
        android:elevation="3dp"
        card_view:cardCornerRadius="@dimen/card_album_radius">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="match_parent"
                android:layout_height="@dimen/album_cover_height"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:clickable="true"
                android:onClick="clikii"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/thumbnail"
                android:paddingLeft="@dimen/album_title_padding"
                android:paddingRight="@dimen/album_title_padding"
                android:paddingTop="@dimen/album_title_padding"
                android:textColor="@color/album_title"
                android:textSize="@dimen/album_title" />

            <TextView
                android:id="@+id/count"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:paddingBottom="@dimen/songs_count_padding_bottom"
                android:paddingLeft="@dimen/album_title_padding"
                android:paddingRight="@dimen/album_title_padding"
                android:textSize="@dimen/songs_count" />

            <ImageView
                android:id="@+id/overflow"
                android:layout_width="@dimen/ic_album_overflow_width"
                android:layout_height="@dimen/ic_album_overflow_height"
                android:layout_alignParentRight="true"
                android:layout_below="@id/thumbnail"
                android:layout_marginTop="@dimen/ic_album_overflow_margin_top"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_dots" />

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

主要活动代码我在哪里使用我的ONCLICK方法...

public void clikii(View view){
        TextView as=(TextView)findViewById(R.id.title);
        String qq=as.getText().toString();


            Toast.makeText(MainActivity.this,qq, Toast.LENGTH_SHORT).show();


    }

enter image description here

当我使用此代码时,只显示所有图像的第一个image_title ..

我想从图像中获取名称并在TOAST消息中显示..任何人请帮助..

2 个答案:

答案 0 :(得分:0)

  Try This   
   public void clikii(View view){
    TextView as=(TextView)findViewById(R.id.title);
             as.setText("yourImageNAme");
    String qq=as.getText().toString();


        Toast.makeText(MainActivity.this,qq, Toast.LENGTH_SHORT).show();


}         

答案 1 :(得分:0)

试试这段代码:

public void clikii(View view){
        ViewGroup parent = (ViewGroup) view;
        TextView as = (TextView) parent.findViewById(R.id.title);
        String qq = as.getText().toString();

        Toast.makeText(MainActivity.this, qq, Toast.LENGTH_SHORT).show();
    }