2015-03-13 45 views
1

我想在片段內部實現警報對話框。
我已經創建了一個名爲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; 
     } 

    } 
} 

,我試過的東西拿到LayoutInflatergetActivity().getLayoutInflater(),但它不起作用。

也許我想通了,也許邏輯不正確,但我不明白爲什麼我做錯了。

這是錯誤:的

enter image description here

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 
+0

通常情況下,我想,內部onAttach(活動動作) FR agment類將活動的值分配給本地Activity實例,然後在片段中的任意位置使用它,而不是調用getActivity方法 – Eenvincible 2015-03-13 15:48:29

回答

5
public class ConfiguracionFragment extends Fragment implements View.OnClickListener { 

Bundle savedInstanceState; 
Context context; 

public ConfiguracionFragment(Context context) { 
    this.context = context; 
} 

TextView tvEmail,tvPswd; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    this.savedInstanceState =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(context, getLayoutInflater(savedInstanceState), 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; 
    } 

} 

}

+0

無法解析符號'context'... – 2015-03-13 16:06:42

+0

在片段中添加上下文變量。 – 2015-03-13 16:08:14

+0

in constructor,this.context = context; – 2015-03-13 16:08:40