我已創建卡片視圖。現在我想顯示帶有點擊圖像名稱的TOAST消息。請幫助...哪些圖像添加Android代碼:點擊圖片時,應該通過其圖片名稱顯示。
我的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();
}
我的XML代碼..
<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();
}
當我用這個代碼僅第一image_title將顯示所有圖像..
我想從圖像獲取的名稱,並顯示在土司的短信..請人幫助..
你可以分享設置點擊監聽器的代碼嗎? – Shaishav
@Shaishav在xml:android:onClick =「clikii」 – X3Btel