这是主要活动
<?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背景
感谢帮助!!!!
答案 0 :(得分:0)
通常,当您想要片段和活动之间的通信时,您将使用接口。在这种情况下,我要做的是为DialogFragment设置一个单独的类,其中包括一个接口定义,当用户选择新的配置文件时,该定义将回调发送到Main活动。由于我还是比较新,我发现自己完成设置很有帮助。这就是我想出来的。
DialogFragment类向MainActivity发送回调并传递drawable资源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
答案 1 :(得分:0)
我在移动设备上尝试这个并且有错误java.lang.NullPointerException:尝试调用接口方法&#39; void com.example.dayyani.finalproject.ChooseProfileDialog $ OnProfileChangeListener.onProfileSelection(int)&#39;在空对象引用上