我还没有找到明确的答案,所以我想我会写一篇文章,看看是否有人能指引我在正确的方向。为什么ImageButton(Android - Java)没有在实际图像上实现onClick
基本上我创建了一个语音备忘录应用程序,并且为本地保存的文件设置了一个'删除'按钮作为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());
}
MORE INFO(但在最终的解决方案不使用):如果我更改了代码充气像这样的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()。
这里是相对布局XML为的ImageButton:
UPDATE:基于由@约翰部落提供的批准答案我已经改正了我的代码。 “修复”是将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>
你没有使用ImageButton,你正在使用RelativeLayout。 'RelativeLayout deleteButton =(RelativeLayout)inflater.inflate(R.layout.image_buttons,null); deleteButton.setId(i + i);'尝试使用ImageButton代替 – jamesc