繁体   English   中英

为什么ImageButton(Android-Java)无法在实际图像上实现onClick

[英]Why is ImageButton (Android - Java) not implementing onClick on actual image

我没有找到明确的答案,所以我想写一篇文章,看看是否有人可以指出我正确的方向。

基本上,我正在创建Voice Notes应用程序,并为本地保存的文件设置了“删除”按钮,作为ImageButton的垃圾桶图像。 看到用户将能够保存他们想要的任意数量的音频文件,我实现了一个循环,在该循环中创建按钮,通过setID设置ID,然后根据生成的ID使用点击监听器。

我的问题是单击侦听器在图像的侧面起作用,而不是在实际的图像上起作用,这意味着如果我单击ImageButton的右侧或左侧,则文件将被删除,但是,当我实际单击其中的垃圾箱图像时ImageButton,什么也没发生。 我也有在单击按钮时随时注册的日志,并且在单击实际图像时它们也不起作用,因此我确定问题是单击ImageButton时不使用监听器。 -请记住,我说过的点击记录在图片的左右两侧。

以下是相关代码:

            try{
            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                    //Button button = new Button(getApplicationContext());
                    TextView fileButton = new TextView(getApplicationContext());
                    fileButton.setTextColor(Color.parseColor("#ffffff"));
                    fileButton.setBackgroundResource(R.drawable.textview_button_colors);
                    fileButton.setTypeface(fileButton.getTypeface(), Typeface.BOLD);
                    fileButton.setText((i+1)+")   "+listOfFiles[i].getName());
                    fileButton.setId(i);
                    fileButton.setPadding(10,10,10,10);//LTRB
                    fileButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);


                    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    RelativeLayout deleteButton = (RelativeLayout) inflater.inflate(R.layout.image_buttons, null);
                    deleteButton.setId(i+i);

                    Log.d(LOG_TAG, String.valueOf(deleteButton.getId()) );//WORKS AS EXPECTED
                    deleteButton.setGravity(Gravity.CENTER_HORIZONTAL);

                    TableRow horizontalButtonTableRow= new TableRow(getApplicationContext());
                    TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams
                            (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);

                    int leftMargin=0;int topMargin=15;int rightMargin=0;int bottomMargin=0;
                    tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);

                    horizontalButtonTableRow.setLayoutParams(tableRowParams);

                    horizontalButtonTableRow.addView(fileButton);
                    horizontalButtonTableRow.addView(deleteButton);


                    final String fileName = saveDirectory.toString() + File.separator +
                            listOfFiles[i].getName();

                    fileButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //Log.d(LOG_TAG, "should be playing audio");
                            MediaPlayer mediaPlayer = new MediaPlayer();
                            try {
                                Vibrator vib = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                                vib.vibrate(25);

                                FileInputStream fis = new FileInputStream(fileName);
                                mediaPlayer.setDataSource(fis.getFD());
                                Log.d(LOG_TAG, fileName);
                                mediaPlayer.prepare();
                            } catch (Exception e) {
                                Log.d(LOG_TAG, String.valueOf(e));
                            }
                            mediaPlayer.start();
                        }
                    });

                    deleteButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {


                            try {
                                Vibrator vib = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                                vib.vibrate(25);

                                File deleteFile = new File(fileName);
                                deleteFile.delete();
                                savedFileLoader();
                                Log.d(LOG_TAG, fileName+": WAS DELETED");

                            } catch (Exception e) {
                                Log.d(LOG_TAG, String.valueOf(e));
                            }
                        }
                    });
                    verticalButtonContainerLayout.addView(horizontalButtonTableRow);
                }//END IF
            }//END FOR
        }catch(Exception e){
            Log.d(LOG_TAG,e.toString());
        }

更多信息(但未在最终解决方案中使用):如果我更改充气机代码以使其工作,则imageButton不再添加到我的视图中。 我认为这是因为我被迫从原始相对视图中删除了图像按钮。

LayoutInflater inflater = 
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                RelativeLayout deleteButtonLayout = (RelativeLayout) 
inflater.inflate(R.layout.image_buttons, null);
                ImageButton deleteButton =  
deleteButtonLayout.findViewById(R.id.imageButton);
                deleteButtonLayout.removeView(deleteButton);
                deleteButton.setId(i+i);

如果我选择不使用removeView,则会出现以下异常:java.lang.IllegalStateException:指定的子代已经有一个父代。 您必须先在孩子的父母上调用removeView()。

这是imageButton在相对布局中的XML:

更新:我已根据@John Tribe提供的已批准答案更正了我的代码。 “解决方案”是将RelativeLayout设置为可点击,同时将ImageButton设置为不可点击。 这样做可以使我的整个展开后的布局都可单击,这正是我看到的这个问题所需要的,因为在我的展开后的RelativeLayout中只有1个元素。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:clickable="true"
    android:layout_height="match_parent">

    <ImageButton

        style="?android:attr/buttonBarStyle"
        android:id="@+id/imageButton"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:adjustViewBounds="true"
        android:clickable="false"

        android:scaleType="center"
        android:background="@drawable/trash"/>

</RelativeLayout>

您使用了RelativeLayout,应该使用Image或ImageButton,但是如果您确实要使用RelativeLayout,则在xml中添加android:clickable="true"

另一种方法是找到ImageButton并设置侦听器。

ImageButton deleteButton = deleteButtonLayout.findViewById(R.id.imageButton);
deleteButton.setOnClickListener( new On...);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM