2swan

Android 메뉴 (목록 대화상자) 본문

Programming/Android Example

Android 메뉴 (목록 대화상자)

2swan 2023. 8. 2. 23:46

 

main1.java

//첫번째 결과 값

public class MainActivity2_dialog extends AppCompatActivity {
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity2_dialog);

        button = findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String[] versionArray = {"파이", "큐(10)", "알(11)"};
                boolean[] checkArray = new boolean[] {true, false, false};
                AlertDialog.Builder alg = new AlertDialog.Builder(MainActivity2_dialog.this);
                alg.setTitle("좋아하는 버전은?");
                alg.setMultiChoiceItems(versionArray, checkArray, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        button.setText(versionArray[i]);
                    }
                });
                alg.setPositiveButton("닫기", null);
                alg.show();
            }
        });


    }
}

 

main2.java

 

//두 번째 결과 값

public class MainActivity2_dialog extends AppCompatActivity {
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity2_dialog);

        button = findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(MainActivity2_dialog.this)
                        .setTitle("제목입니다")
                        .setMessage("이곳이 내용")
                        .show();
                AlertDialog.Builder dlg  = new AlertDialog.Builder(MainActivity2_dialog.this);
                dlg.setTitle("제목입니다.");
                dlg.setMessage("이곳이 내용입니다");
                dlg.setPositiveButton("확인", new DialogInterface.OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity2_dialog.this,
                                "확인클릭", Toast.LENGTH_LONG).show();
                    }
                });
                 dlg.setNegativeButton("취소", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity2_dialog.this,
                                "취소클릭",Toast.LENGTH_LONG).show();
                     }
                 });
                dlg.show();
           }
        });

    }
}

 

main.xml (공통)

//두 결과 값에 대해서 공통적으로 사용

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2_dialog"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="대화상자"/>

</LinearLayout>

결과 값(첫 번째)

 

 

결과 값(두 번째)