0
我試圖實現顯示DialogFragment的自定義LinearLayout。我做了所有的工作,而且效果很好,Dialog顯示並且工作完美,但我似乎無法從DialogFragment獲取數據。自定義LinearLayout調用方法
所以我創建了一個接口:
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我這樣做,但活動永諾空:
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()BU他們都是空的。
您可以添加所有的代碼,會更清楚 – Stefan
我已經添加了全班同學。 –