私はこれについて明確な答えを見つけていないので、誰かが正しい方向で私を指すことができるかどうかを確認するために投稿を書きます。ImageButton(Android-Java)が実際の画像にonClickを実装していないのはなぜですか。
私は基本的にVoice Notesアプリケーションを作成しています。ImageButtonのゴミ箱イメージとして、ローカルに保存されたファイルの「削除」ボタンを設定しました。ユーザーが必要なだけ多くのオーディオファイルを保存できるので、ボタンが作成されたループを実装すると、IDはsetIDで設定され、生成された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()を呼び出す必要があります。
UPDATE:ここ
は、相対的なレイアウトでIMAGEBUTTONためのXMLである私は@ジョン・トライブが提供する承認された回答に基づいて自分のコードを修正しました。 「修正」は、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を使用しているImageButtonを使用していません。 'RelativeLayout deleteButton =(RelativeLayout)インフレータ.inflate(R.layout.image_buttons、null); deleteButton.setId(i + i); '代わりにImageButtonを使用してください – jamesc