在AlertDialog的元素之间的填充区域中聆听触摸

时间:2018-01-05 22:23:58

标签: java android

我有(自定义)标题区域和AlertDialog的消息区域的onTouchListeners。

我试图设置对话框,以便用户可以盲目地点击AlertDialog的右上角1/4来切换是否播放背景音乐作为可点击区域的扬声器会导致命中区/区域太小。

我的问题是:用红色标记的邮件和标题之间的区域不是处理ontouchlistener事件

大多数人会建议创建一个自定义对话框,但问题是我真的喜欢这个对话框看起来的方式(它有一个非常有库存的材料设计审美)并且已经跳过很多箍以使它看起来完全一样我喜欢(在一个看不见的中性按钮上绘制排行榜,自定义标题区域)。我不想制作自定义对话框,除非我能让它看起来与我现在的完全相同(很难模仿库存材料对话框的外观,相信我,我已经尝试并做了很多研究/浪费了很多时间来尝试)。

我假设自定义标题区域和消息区域的触摸事件不包含或说明其间的边距或填充。

请原谅恶心的代码!!我只是试图将所有东西一起破解并在以后整理它。

提前致谢!

Screenshot

我的自定义标题区域的线性布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_margin="0px"
    android:paddingTop="0px"
    android:paddingLeft="0px"
    android:paddingRight="0px"
    android:paddingBottom="0px" 
    android:orientation="horizontal"
    android:id="@+id/st"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="24dp"
        android:paddingTop="20dp"
        android:paddingRight="5dp"
        android:src="@drawable/ic_pause"/>


    <TextView
        android:id="@+id/leaderboard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#800080"
        android:textSize="22sp"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle"
        android:layout_gravity="center"
        android:paddingTop="18dp"
        android:layout_weight="1"
        android:text="Paused"

        />
    <ImageView
        android:id="@+id/soundtoggle"
        style="?android:attr/panelTextAppearance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="24dp"
        android:paddingTop="20dp"
        android:paddingRight="?android:dialogPreferredPadding"
        android:gravity="right"/>

</LinearLayout>

我的Android代码

AlertDialog.Builder ad = new AlertDialog.Builder(new ContextThemeWrapper(AndroidLauncher.this, android.R.style.Theme_Material_Light_Dialog))
                        .setMessage(msg)
                        .setCustomTitle(myLayout)
                        .setCancelable(false)
                        .setNegativeButton("End Game", new  DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                pauseInterface.end();
                                dialog.cancel();
                            }
                        })
                        .setNeutralButton("   ", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //startSignInIntent();
                                showLeaderboard();
                                dialog.cancel();
                            }
                        })
                        .setPositiveButton("Resume", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                pauseInterface.resume();
                                dialog.cancel();
                            }
                        });


                alertDialog = ad.create();

                int titleId = getResources().getIdentifier("alertTitle", "id", "android");
                if (titleId > 0) {
                    TextView dialogTitle = (TextView) alertDialog.findViewById(titleId);

                }

                alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

                    @Override
                    public void onShow(DialogInterface dialogInterface) {
                        Button button = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
                        Drawable drawable = getResources().getDrawable(R.drawable.ic_leaderboard);
                        drawable.setBounds((int) 0,
                                0, (int) (drawable.getIntrinsicWidth() * 1),
                                drawable.getIntrinsicHeight());
                        button.setCompoundDrawables(drawable, null, null, null);
                    }
                });

                alertDialog.show();
                final ImageView soundToggle = (ImageView) alertDialog.findViewById(R.id.soundtoggle);
                if (tetrisgame.getMusicState()) {
                    if (tetrisgame.getMusicState()) {
                        soundToggle.setImageDrawable(getResources().getDrawable(R.drawable.music_on, getApplicationContext().getTheme()));

                    } else {
                        soundToggle.setImageDrawable(getResources().getDrawable(R.drawable.music_off, getApplicationContext().getTheme()));
                    }
                    TextView tv = (TextView) alertDialog.findViewById(android.R.id.message);
                    tv.setOnTouchListener(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent m) {

                            if (m.getAction() == MotionEvent.ACTION_DOWN && (m.getX() >= (v.getRight() - (v.getWidth() / 4)))) {
                                Log.println(Log.ERROR, "Ken", "Popular Pothead");
                                if (tetrisgame.toggleMusic()) {
                                    String uri = "@drawable/music_on";  // where myresource (without the extension) is the file

                                    int imageResource = getResources().getIdentifier(uri, null, getPackageName());


                                    Drawable res = getResources().getDrawable(imageResource);
                                    soundToggle.setImageDrawable(res);
//                            soundToggle.setImageDrawable(getResources().getDrawable(R.drawable.music_off, alertDialog.findViewById(android.R.id.message).getTheme()));

                                } else {
                                    soundToggle.setImageDrawable(getResources().getDrawable(R.drawable.music_off, null));

                                }

                            }
                            return true;
                        }

                    });

                    final LinearLayout as = (LinearLayout) alertDialog.findViewById(R.id.st);
                    as.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent m) {

                            if (m.getAction() == MotionEvent.ACTION_DOWN) {
                                Log.println(Log.ERROR, "Ken", "Tweed");
                                if (tetrisgame.toggleMusic()) {
                                    String uri = "@drawable/music_on";  // where myresource (without the extension) is the file

                                    int imageResource = getResources().getIdentifier(uri, null, getPackageName());


                                    Drawable res = getResources().getDrawable(imageResource);
                                    soundToggle.setImageDrawable(res);
//                            soundToggle.setImageDrawable(getResources().getDrawable(R.drawable.music_off, alertDialog.findViewById(android.R.id.message).getTheme()));

                                } else {
                                    soundToggle.setImageDrawable(getResources().getDrawable(R.drawable.music_off, null));

                                }
                            }
                            return true;
                        }
                    });

0 个答案:

没有答案