如何显示对话片段?
How do I show a DIalog Fragment?
您好,我想知道如何在用户单击按钮时在屏幕上显示对话框;
java 文件;
public class Example extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_layout);
findViewById(R.id.exampleButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Jomin","sdsd");
ExampleDialogFragment CallUs = new ExampleDialogFragment();
CallUs.getDialog();
}
});
}
}
这是 ExampleDialogFragment java 文件;
public class ExampleDialogFragment extends android.support.v4.app.DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(false); builder.setTitle(R.string.LogOutTitle).setMessage(R.string.LogOutMessage).setPositiveButton(R.string.LogOutPositive,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Uri number = Uri.parse("tel:0845 3000 116");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);
}
});
AlertDialog dialog = builder.create();
return dialog;
}
}
有人能告诉我当用户单击 ID 为 exampleButton 的按钮时如何显示此对话框。
这是一个很好的指南:http://android-developers.blogspot.com/2012/05/using-dialogfragments.html
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
// ...
public class FragmentDialogDemo extends FragmentActivity implements EditNameDialogListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showEditDialog();
}
private void showEditDialog() {
FragmentManager fm = getSupportFragmentManager();
EditNameDialog editNameDialog = new EditNameDialog();
editNameDialog.show(fm, "fragment_edit_name");
}
}
callUs.show(getFragmentManager(), "SOME_TAG");
而且我认为您必须将 activity 切换到 FragmentActivity 并将 getFragmentManager() 替换为 getSupportFragmentManager() 因为您的 DialogFragment 扩展了支持库的版本
您好,我想知道如何在用户单击按钮时在屏幕上显示对话框;
java 文件;
public class Example extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_layout);
findViewById(R.id.exampleButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Jomin","sdsd");
ExampleDialogFragment CallUs = new ExampleDialogFragment();
CallUs.getDialog();
}
});
}
}
这是 ExampleDialogFragment java 文件;
public class ExampleDialogFragment extends android.support.v4.app.DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(false); builder.setTitle(R.string.LogOutTitle).setMessage(R.string.LogOutMessage).setPositiveButton(R.string.LogOutPositive,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Uri number = Uri.parse("tel:0845 3000 116");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);
}
});
AlertDialog dialog = builder.create();
return dialog;
}
}
有人能告诉我当用户单击 ID 为 exampleButton 的按钮时如何显示此对话框。
这是一个很好的指南:http://android-developers.blogspot.com/2012/05/using-dialogfragments.html
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
// ...
public class FragmentDialogDemo extends FragmentActivity implements EditNameDialogListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showEditDialog();
}
private void showEditDialog() {
FragmentManager fm = getSupportFragmentManager();
EditNameDialog editNameDialog = new EditNameDialog();
editNameDialog.show(fm, "fragment_edit_name");
}
}
callUs.show(getFragmentManager(), "SOME_TAG");
而且我认为您必须将 activity 切换到 FragmentActivity 并将 getFragmentManager() 替换为 getSupportFragmentManager() 因为您的 DialogFragment 扩展了支持库的版本