![](/img/trans.png)
[英]How to make an Imagebutton change Image onClick in and Back to its original on ClickOut in Java/Android Studio
[英]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.