【问题标题】:i want to change imageButton backgroun from dialog我想从对话框更改图像按钮背景
【发布时间】:2016-09-11 11:39:31
【问题描述】:

这是主要活动

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dayyani.finalproject.MainActivity">

<ImageButton
    android:id="@+id/imageSelection"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10sp"
    android:layout_width="80sp"
    android:layout_height="80sp"
    android:background="@drawable/profile"
    android:onClick="choseProfile"/>
<EditText
    android:id="@+id/firestEdit"
    android:layout_below="@+id/imageSelection"
    android:layout_centerHorizontal="true"
    android:layout_width="200sp"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"/>
<EditText
    android:id="@+id/secondtEdit"
    android:layout_below="@+id/firestEdit"
    android:layout_centerHorizontal="true"
    android:layout_width="200sp"
    android:layout_height="wrap_content"/>

 package com.example.dayyani.finalproject;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;


public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageButton imageSelection = (ImageButton) findViewById(R.id.imageSelection);
    EditText firstEdit = (EditText) findViewById(R.id.firestEdit);
    EditText secondtEdit = (EditText) findViewById(R.id.secondtEdit);
}


public void choseProfile(View v) {
    LayoutInflater inflater = this.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.dialog_selection, null);
    final AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("chose profile")
            .setView(dialogView)
            .setNegativeButton("back", null)
            .show();

}

public void changeImageBackground(View v) {
    View.OnClickListener change = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final ImageButton imageSelection = (ImageButton) findViewById(R.id.imageSelection);
            final ImageButton firstImage = (ImageButton) findViewById(R.id.firstImage);
            ImageButton secondImage = (ImageButton) findViewById(R.id.secondImage);
            ImageButton thirdImage = (ImageButton) findViewById(R.id.thirdImage);
            ImageButton fourthImage = (ImageButton) findViewById(R.id.fourthImage);


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

                   imageSelection.setBackground(getDrawable(R.drawable.profile));
               }
           });

            secondImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    imageSelection.setBackground(getDrawable(R.drawable.profile2));
                }
            });

        }
    };
}

}

这是对话框

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageButton
    android:id="@+id/firstImage"
    android:layout_width="80sp"
    android:layout_height="80sp"
    android:background="@drawable/profile"
    android:onClick="changeImageBackground"
    android:clickable="true"/>
<TextView
    android:id="@+id/firstText"
    android:layout_toRightOf="@+id/firstImage"
    android:layout_width="250sp"
    android:layout_height="80sp"
    android:text="firstImage"/>
<ImageButton
    android:id="@+id/secondImage"
    android:layout_below="@+id/firstImage"
    android:layout_width="80sp"
    android:layout_height="80sp"
    android:background="@drawable/profile2"
    android:onClick="changeImageBackground"
    android:clickable="true"/>
<TextView
    android:id="@+id/secondText"
    android:layout_toRightOf="@+id/secondImage"
    android:layout_below="@+id/firstText"
    android:layout_width="250sp"
    android:layout_height="80sp"
    android:text="secondImage"/>
<ImageButton
    android:id="@+id/thirdImage"
    android:layout_below="@+id/secondImage"
    android:layout_width="80sp"
    android:layout_height="80sp"
    android:clickable="true"/>
<TextView
    android:id="@+id/thirdText"
    android:layout_toRightOf="@+id/thirdImage"
    android:layout_below="@+id/secondText"
    android:layout_width="250sp"
    android:layout_height="80sp"
    android:text="thirdImage"/>
<ImageButton
    android:id="@+id/fourthImage"
    android:layout_below="@+id/thirdImage"
    android:layout_width="80sp"
    android:layout_height="80sp"
    android:clickable="true"/>
<TextView
    android:id="@+id/fourthText"
    android:layout_toRightOf="@+id/fourthImage"
    android:layout_below="@+id/thirdText"
    android:layout_width="250sp"
    android:layout_height="80sp"
    android:text="thirdImage"/>


</RelativeLayout>

我想要的是,如果我单击对话框上的图像,他将使用此图像背景并将其放在活动图像上

例如,当我在对话框中单击 firstImage 时,imageSelection 背景更改为 firstImage 背景

感谢您的帮助!!!!

【问题讨论】:

    标签: android background dialog android-alertdialog imagebutton


    【解决方案1】:

    通常,当您希望在 Fragment 和 Activity 之间进行通信时,您会使用接口。在这种情况下,我要做的是为您的 DialogFragment 设置一个单独的类,其中包括一个接口定义,当用户选择一个新的配置文件时,该接口将回调发送到 Main 活动。由于我在这方面还比较陌生,我发现自己完成设置很有帮助。这是我想出的。

    DialogFragment 类向 MainActivity 发送回调,并通过可绘制资源 ID:

    public class ChooseProfileDialog extends DialogFragment{
    
    ImageButton imageSelection;
    ImageButton firstImage;
    ImageButton secondImage;
    
    
    private OnProfileChangeListener mProfileChangeListener;
    
    public ChooseProfileDialog() {
    }
    
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.dialog_selection, null);
        final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity())
                .setTitle("chose profile")
                .setView(dialogView)
                .setNegativeButton("back", null);
    
        imageSelection = (ImageButton) dialogView.findViewById(R.id.imageSelection);
        firstImage = (ImageButton) dialogView.findViewById(R.id.firstImage);
        secondImage = (ImageButton) dialogView.findViewById(R.id.secondImage);
    
    
        // Callbacks to MainActivity
        firstImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Call onProfileSelection in the MainActivity and pass the resource ID
                mProfileChangeListener.onProfileSelection(R.drawable.profile);
                dismiss();
            }
        });
    
        secondImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Call onProfileSelection in the MainActivity and pass the resource ID
                mProfileChangeListener.onProfileSelection(R.drawable.profile2);
                dismiss();
            }
        });
    
    
        return dialog.create();
    }
    
    
    public interface OnProfileChangeListener {
        // method called
        public void onProfileSelection(int backgroundResourceId);
    }
    
    
    // When the dialog attaches to MainActivity, check that the interface has been implemented
    
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    
        // Check that the MainActivity implements your interface
        try {
            mProfileChangeListener = (OnProfileChangeListener) getActivity();
        } catch (ClassCastException e){
            throw new ClassCastException(getContext().toString() + "must implement the OnProfilechangeListener interface.");
        }
    
    
    }
    

    }

    MainActivity 实现接口并覆盖 onProfileSelection 方法:

    public class MainActivity extends AppCompatActivity implements ChooseProfileDialog.OnProfileChangeListener {
    
    ImageButton imageSelection;
    EditText firstEdit;
    EditText secondtEdit;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        imageSelection = (ImageButton) findViewById(R.id.imageSelection);
        firstEdit = (EditText) findViewById(R.id.firestEdit);
        secondtEdit = (EditText) findViewById(R.id.secondtEdit);
    }
    
    
    public void choseProfile(View v) {
        ChooseProfileDialog dialog = new ChooseProfileDialog();
        dialog.show(getSupportFragmentManager(),"CHOOSE_PROFILE_TAG");
    }
    
    // Define what happens when the callback from ChooseProfileDialog in invoked
    @Override
    public void onProfileSelection(int backgroundResourceId) {
        imageSelection.setImageResource(backgroundResourceId);
    }
    }
    

    这也可能有用:https://developer.android.com/training/basics/fragments/communicating.html

    【讨论】:

    • 我得到一个错误错误:(31, 15) 错误: 找不到合适的方法用于 show(android.support.v4.app.FragmentManager,String) 方法 DialogFragment.show(android.app.FragmentManager ,String) 不适用(参数不匹配;android.support.v4.app.FragmentManager 无法转换为 android.app.FragmentManager)方法 DialogFragment.show(FragmentTransaction,String) 不适用(参数不匹配;android.support.v4 .app.FragmentManager 无法转换为 FragmentTransaction)
    • 它的工作......问题是主要活动需要将“extend AppCompatActivity”更改为“extend Activity”,然后在“choseProfile”方法中将“getSupportFragmentManager”更改为“getFragmentManager”谢谢非常!!!!
    • 0 否决票接受我在手机上尝试这个并且有错误 java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.dayyani.finalproject.ChooseProfileDialog$OnProfileChangeListener.onProfileSelection(int )' 在空对象引用上
    • 听起来片段没有在 onAttach() 中正确初始化 mProfileChangeListener 变量。你在 onAttach 中的代码和我上面贴的一样吗?
    【解决方案2】:

    我在移动设备上尝试此操作,但出现错误 java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.dayyani.finalproject.ChooseProfileDialog$OnProfileChangeListener.onProfileSelection(int)' on an null object reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-07
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      相关资源
      最近更新 更多