2016-09-23 101 views
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他們都是空的。

+0

您可以添加所有的代碼,會更清楚 – Stefan

+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); 
}}