对话框中的ImageView setOnClickListener

时间:2015-06-17 21:05:17

标签: android dialog imageview onclicklistener

我使用此方法创建自定义对话框:

public Dialog onCreateDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.dialog_rate, null))
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    SharedPreferences.Editor settingsEditor = settings.edit();
                    settingsEditor.putBoolean("rate",false);
                    settingsEditor.apply();
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.addCategory(Intent.CATEGORY_BROWSABLE);
                    intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=de.resper.pzcrettungsdienst"));
                    startActivity(intent);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    SharedPreferences.Editor settingsEditor = settings.edit();
                    settingsEditor.putBoolean("rate",false);
                    settingsEditor.apply();
                }
            })
            .setNeutralButton(R.string.later, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    SharedPreferences.Editor settingsEditor = settings.edit();
                    settingsEditor.putInt("count",0);
                    settingsEditor.apply();
                }
            });
    return builder.create();
}

我打开这样的对话框:

Dialog dialog = onCreateDialog();
dialog.show(); 

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="10dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/dialog_message"
        android:id="@+id/textView15"
        android:layout_gravity="center_horizontal"
        android:gravity="center"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:gravity="center"
        android:text="@string/resperLink"
        android:layout_margin="20dp"
        android:id="@+id/textView" />


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/fbImg"
            android:src="@drawable/fb_icon"
            android:layout_margin="5dp"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/gplusImg"
            android:src="@drawable/gplus_icon"
            android:layout_margin="5dp"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/twImg"
            android:src="@drawable/tw_icon"
            android:layout_margin="5dp"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/pinImg"
            android:src="@drawable/pin_icon"
            android:layout_margin="5dp"/>


    </LinearLayout>

</LinearLayout>

在布局文件中是图像,是否可以在布局“setOnClickListener”中设置图像?按钮工作,但我想为图像添加更多“动作”。

1 个答案:

答案 0 :(得分:3)

  

在布局文件中是图像,是否可以在图像中设置   布局&#34; setOnClickListener&#34;?

是的,这是可能的。保留对膨胀视图的引用,并将其用于findViewById

View view= inflater.inflate(R.layout.dialog_rate, null);
view.findViewById(R.id.yourid).setOnClickListener(...);
builder.setView(view)