最後に、私は問題が何であるかを発見しました...
次のような関数がある場合:
public void test()
{
DialogFragment dlg = new LoginDialog();
dlg.show(getSupportFragmentManager(), "login");
}
このダイアログは、test() が終了したときにのみ表示されます。これが Android ダイアログが機能する唯一の方法かどうかはわかりませんが、これについて詳しく読むことは間違いありません...
元の質問:
私はAndroidの世界が初めてです。誰かが光を当てることができますか?
dlg.show() は例外なく実行されましたが、何も起こりません。何が問題なのかを知るにはどうすればよいですか? プロジェクトは Android 2.2 の API を使用しています。
public class MainActivity extends FragmentActivity
{
...
DialogFragment dlg = new LoginDialog();
dlg.show(getSupportFragmentManager(), "login");
}
ダイアログのレイアウト:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="username" />
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:hint="password"/>
</LinearLayout>
ダイアログクラス:
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
public class LoginDialog extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.login_dialog, null))
// Add action buttons
.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do something
}
});
return builder.create();
}
}