package com.example.sample19;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class InputNameDialogFragment extends DialogFragment {
private String dialogTitle;
public void
setDialogtitle(String title){
dialogTitle=title;
}
protected static
interface InputNameDialogListener{
void onFinishInputDialog(String inputText);
}
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle
savedInstanceState) {
View
rootView=inflater.inflate(R.layout.dialog_fragment_input_name,
container,false);
final EditText
txtName=(EditText)rootView.findViewById(R.id.txtName);
Button
btnDone=(Button)rootView.findViewById(R.id.btnDone);
btnDone.setOnClickListener(new
View.OnClickListener() {
@Override
public void
onClick(View v) {
InputNameDialogListener
mainActivity=(InputNameDialogListener)getActivity();
mainActivity.onFinishInputDialog(txtName.getText().toString());
dismiss();
}
});
getDialog().setTitle(dialogTitle);
return rootView;
}
}
package com.example.sample19;
import
com.example.sample19.InputNameDialogFragment.InputNameDialogListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements
InputNameDialogFragment.InputNameDialogListener{
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState==null){
PlaceholderFragment
placeholderFragment=new PlaceholderFragment();
FragmentManager
fragmentManager=getFragmentManager();
FragmentTransaction
fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.mainContainer,
placeholderFragment);
fragmentTransaction.commit();
}
}
public void
onFinishInputDialog(String inputText){
Toast.makeText(this,
inputText, Toast.LENGTH_SHORT).show();
}
public static class
PlaceholderFragment extends Fragment{
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle
savedInstanceState) {
View
rootView=inflater.inflate(R.layout.fragment_main, container, false);
Button
showDialogBtn=(Button)rootView.findViewById(R.id.showDialogBtn);
showDialogBtn.setOnClickListener(new
View.OnClickListener() {
@Override
public
void onClick(View v) {
FragmentManager
fragmentManager=getActivity().getFragmentManager();
InputNameDialogFragment
inputNameDialog=new InputNameDialogFragment();
inputNameDialog.setDialogtitle("MyInput
Dialog");
inputNameDialog.show(fragmentManager,
"input dialog");
}
});
Button
showYesDialogBtn=(Button)rootView.findViewById(R.id.showYesDialogBtn);
showYesDialogBtn.setOnClickListener(new
View.OnClickListener() {
@Override
public
void onClick(View v) {
//
TODO Auto-generated method stub
}
});
return
rootView;
}
}
}
沒有留言:
張貼留言