Android Kotlin
Android Kotlin - AlertDialog example
누르님
2018. 8. 17. 12:04
android studio ver. 3.1.4
kotlin_version = 1.2.60
AlertDialog 예제입니다.
먼저 Java 간단한 다이얼로그 예제를 작성하였습니다.
public class SplashActivity_j extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); FrameLayout lay = findViewById(R.id.lay_push); lay.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.lay_push: AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(SplashActivity_j.this, R.style.Theme_AppCompat_Light_Dialog_Alert)); builder.setTitle("제목"); builder.setMessage("메시지 내용"); builder.setPositiveButton("확인", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { } }); builder.setNegativeButton("취소", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { } }); builder.show(); break; } } }
아래는 코틀린으로 변환된 코드입니다.
{ dialog , id 는
{ _ , _ 이렇게 사용도 가능합니다.
class SplashActivity : AppCompatActivity(), View.OnClickListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_splash) // lay 클릭시 val lay:FrameLayout = findViewById(R.id.lay_push) lay.setOnClickListener(this) } override fun onClick(v: View) { when(v.id) { R.id.lay_push -> { // 다이얼로그 val builder = AlertDialog.Builder(ContextThemeWrapper(this@SplashActivity, R.style.Theme_AppCompat_Light_Dialog)) builder.setTitle("제목(kotlin)") builder.setMessage("내용(Kotlin)") /*builder.setPositiveButton("확인") {dialog, id -> } builder.setNegativeButton("취소") {dialog, id -> }*/ builder.setPositiveButton("확인") { _, _ -> d("alert ok") } builder.setNegativeButton("취소") { _, _ -> d("alert cancel") } builder.show() } } } }