-
-
Save codingwithsara/adbd346ce8ba901f5db018fc21f18606 to your computer and use it in GitHub Desktop.
AlertDialog Customize 2-2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.codeforfun.customalertdialog; | |
import android.app.AlertDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// カスタムレイアウトの用意 | |
LayoutInflater layoutInflater = getLayoutInflater(); | |
View customAlertView = layoutInflater.inflate(R.layout.custom_alert_dialog, null); | |
AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
builder.setView(customAlertView); | |
// タイトルの変更 | |
TextView title = customAlertView.findViewById(R.id.title); | |
title.setText("こんにちは!"); | |
// メッセージの変更 | |
TextView message = customAlertView.findViewById(R.id.message); | |
message.setText("ここにメッセージを入力します。ここにメッセージを入力します。ここにメッセージを入力します。"); | |
final AlertDialog alertDialog = builder.create(); | |
// ボタンの設定 | |
Button alertBtn = customAlertView.findViewById(R.id.btnPositive); | |
alertBtn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// ボタンを押した時の処理を書く | |
// ダイアログを閉じる | |
alertDialog.dismiss(); | |
} | |
}); | |
// ダイアログ表示 | |
alertDialog.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment