我想在片段內部實現警報對話框。
我已經創建了一個名爲MyDialog
新的類,它包含:getLayoutInflater()在片段內
public class MyDialog {
public static AlertDialog.Builder create(final Context context, final LayoutInflater layoutInflater, final String title, final String content) {
View view = layoutInflater.inflate(R.layout.userdialog, null);
((TextView)view.findViewById(R.id.textViewTitleDialog)).setText(title);
((TextView)view.findViewById(R.id.textViewContentDialog)).setText(content);
return new AlertDialog.Builder(context).setView(view);
}
}
然後在的時候,我想用它,我調用這個類,如:
AlertDialog.Builder myDialog = MyDialog.create(this, getLayoutInflater(), R.string.cambioemail, R.string.nuevoemail);
myDialog.setPositiveButton(R.string.canviaruserdialog, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(R.string.cancelaruserdialog, null)
.show();
片段的完整代碼:
public class ConfiguracionFragment extends Fragment implements View.OnClickListener {
public ConfiguracionFragment() {
}
TextView tvEmail,tvPswd;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
tvEmail = (TextView) rootView.findViewById(R.id.tvEmailChange);
tvPswd = (TextView) rootView.findViewById(R.id.tvPswdChange);
tvEmail.setOnClickListener(this);
tvPswd.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tvEmailChange:
AlertDialog.Builder myDialog = MyDialog.create(this, getLayoutInflater(), R.string.cambioemail, R.string.nuevoemail);
myDialog.setPositiveButton(R.string.canviaruserdialog, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(R.string.cancelaruserdialog, null)
.show();
break;
case R.id.tvPswdChange:
Intent intent2 = new Intent();
intent2.setClass(getActivity(), UserDialog.class);
intent2.putExtra("PSWD", "PSWD");
startActivity(intent2);
break;
}
}
}
,我試過的東西拿到LayoutInflater
是getActivity().getLayoutInflater()
,但它不起作用。
也許我想通了,也許邏輯不正確,但我不明白爲什麼我做錯了。
這是錯誤:的
EDIT
Error:(40, 69) error: method getLayoutInflater in class Fragment cannot be applied to given types;
required: Bundle
found: no arguments
reason: actual and formal argument lists differ in length
通常情況下,我想,內部onAttach(活動動作) FR agment類將活動的值分配給本地Activity實例,然後在片段中的任意位置使用它,而不是調用getActivity方法 – Eenvincible 2015-03-13 15:48:29