私は独学の初心者であり、忍耐に感謝します! ありがとう!
Eclipse で、独自の xml ファイル ("custom_dialog") を使用してカスタム アラート ダイアログを作成しました。これは "usernamealert" と呼ばれます。
ユーザーがまだユーザー名を入力していない場合 (つまり、username.length == 0) にアラートがポップアップするようにします。
このレイアウト内には、textView (「あなたの名前は何ですか?」)、editText、およびボタン (「usernameButton」) があります。
ボタンの onclicklistener を配置する前は、すべてが機能していました。これは私の(関連する)Javaでした:
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) getCurrentFocus());
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
usernamebuilder.setView(dialoglayout);
AlertDialog usernamealert = usernamebuilder.create();
onclicklistenerを入れたら壊れた!どこに置くべきだったの?
(以下は私が試したものです...すべて私のOnCreateで)
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)getCurrentFocus());
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
usernamebuilder.setView(dialoglayout);
Button usernameButton = (Button) usernamealert.findViewById(R.id.usernameButton);
usernameButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//store username in sharedprefs
usernamealert.dismiss();
}
});
コードの後に私は言った:
if (username.length() == 0) {
usernamealert.show();
}
繰り返しますが、ボタンをいじり始める前に機能しました!!