我正在尝试实现一个显示DialogFragment的自定义LinearLayout。我做了所有,并且效果很好,Dialog显示并且工作正常,但我似乎无法从DialogFragment获取数据。
所以我创建了一个界面:
public interface DialogCommunicator {void onDismiss(String format, String data);}
在LinearLayout上,我实现了DialogCommunicator接口并完成了这个:
public class CustomLayout extends LinearLayout implements DialogCommunicator, View.OnClickListener {
Button showDialog;
TextView displayUserInput;
CreateDialog dialog;
public CustomLayout(Context context) {
super(context);
}
public CustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
showDialog = (Button) findViewById(R.id.showDialog);
displayUserInput = (TextView) findViewById(R.id.displayUserInput);
showDialog.setOnClickListener(this);
}
@Override
public void onDismiss(String format, String data) {
dialog.dismiss();
someMethod(format, data);
}
private void someMethod(String format, String data) {
displayUserInput.setText(format + data);
}
@Override
public void onClick(View v) {
AppCompatActivity activity = (AppCompatActivity) getContext();
dialog = new CreateDialog();
dialog.show(activity.getSupportFragmentManager(), "Mydialog");
}}
在DialogFragment上我做了这个,但是活动总是为null:
public class CreateDialog extends DialogFragment implements DialogCommunicator, View.OnClickListener {
View view;
String format, data;
Button create;
EditText userFormat, userData;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.create_barcode, container);
userFormat = (EditText) view.findViewById(R.id.userFormat);
userData = (EditText) view.findViewById(R.id.userData);
create = (Button) view.findViewById(R.id.create);
create.setOnClickListener(this);
return view;
}
@Override
public void onDismiss(String format, String data) {
RTToolbarHorizontalLayout layout = (RTToolbarHorizontalLayout) getHost();
if (layout != null) {
layout.onDismiss(format, data);
}
}
@Override
public void onClick(View v) {
format = userFormat.getText().toString();
data = userData.getText().toString();
onDismiss(format, data);
}
}
我尝试过getContext(),getDialog(),getHost(),但它们都是null。
答案 0 :(得分:0)
我使用了广播接收器,因此将数据从DialogFragment发送到布局。这是CustomLayout:
public class CustomLayout extends LinearLayout implements View.OnClickListener {
Button showDialog;
TextView displayUserInput;
CreateDialog dialog;
BroadcastReceiver receiver = new BroadcastReceiver() {
String format = null;
String data = null;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra("format") != null)
format = intent.getStringExtra("format");
if (intent.getStringExtra("barcode") != null)
data = intent.getStringExtra("barcode");
if (format != null && data != null) {
someMethod(format, data);
format = null;
data = null;
}
}
};
public CustomLayout(Context context) {
super(context);
getContext().registerReceiver(receiver,
new IntentFilter("com.exemple.myapplication.DATA_SEND"));
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
getContext().unregisterReceiver(receiver);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
showDialog = (Button) findViewById(R.id.showDialog);
displayUserInput = (TextView) findViewById(R.id.displayUserInput);
showDialog.setOnClickListener(this);
}
private void someMethod(String format, String data) {
displayUserInput.setText(format + data);
}
@Override
public void onClick(View v) {
AppCompatActivity activity = (AppCompatActivity) getContext();
dialog = new CreateDialog();
dialog.show(activity.getSupportFragmentManager(), "MyDialog");
}}
这是DialogFragment中的实现:
public class CreateDialog extends DialogFragment implements View.OnClickListener {
View view;
String format, data;
Button create;
EditText userFormat, userData;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.create_barcode, container);
userFormat = (EditText) view.findViewById(R.id.userFormat);
userData = (EditText) view.findViewById(R.id.userData);
create = (Button) view.findViewById(R.id.create);
create.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
format = userFormat.getText().toString();
data = userData.getText().toString();
Intent i = new Intent("com.exemple.myapplication.DATA_SEND");
i.putExtra("format", format);
i.putExtra("data", data);
getContext().sendBroadcast(i);
}}