2016-04-08 9 views
1

私のアプリでは、私は少なくとも3つのAlertDialogメソッドを作成するのが良いと思っていたので、 の中にユーザーテキスト入力を使ってAlertDialogを作成しようとしています。値のユーザーを返しますHERESにコード"グローバル" AlertDialogを作成する

私のコードのサンプルの書き込みをたくさん節約具体的な方法にして、このことによってそれ に入力した

public void alrtDialog(String setTitle, String setPositiveBtn) 
{ 
    AlertDialog.Builder myDialog = new AlertDialog.Builder(context);//<--create alert dialog builder 
    myDialog.setTitle(setTitle); 
    final EditText myText = new EditText(context); 
    myText.setInputType(InputType.TYPE_CLASS_TEXT); 
    myDialog.setView(myText); 
    Log.e("blalba", "texoutt:" + myText.getText().toString()); 
    myDialog.setPositiveButton(setPositiveBtn, new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      setMsg(myText.getText().toString());//<--set 
      Log.e("blalba", "teinxt:" + myText.getText().toString()); 
     } 
    }); 
    myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 
    myDialog.show(); 
    return getMsg(); 
} 
+0

それはあなたを、スイートた場合にだけ、そのどこにでも – tyczj

+0

ノートの回答を使用し、その後DialogFragemntを作成します。ちょうどチェックマークをメモするには... – GensaGames

答えて

2

DialogFragmentを作成します。
public class MyDialogFragment extends DialogFragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_sample_dialog, container, false); 
     getDialog().setTitle("Simple Dialog"); 
     return rootView; 
    } 
} 

任意のコンテキストに表示

FragmentManager fm = getFragmentManager(); 
MyDialogFragment dialogFragment = new MyDialogFragment(); 
dialogFragment.show(fm, "Sample Fragment"); 
関連する問題