AlertDialog를 이용하여 RatingBar 띄우기

class MyOnLongClickListener implements View.OnLongClickListener {
    @Override
    public boolean onLongClick(View view) {
        if(view.getId()==R.id.button3) {
            showDialog();
            return true;
        }
        return false;
    }
}

LongClick 후에 RatingBar를 띄우기 위해 다음과 리스너를 생성한다.

public void showDialog() {
        final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
        final RatingBar ratingBar = new RatingBar(this);
        ratingBar.setMax(5);
 
        popDialog.setTitle("RatingBar");
        popDialog.setView(ratingBar);
 
        popDialog.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.cancel();
                            }
                        });
 
        popDialog.create();
        popDialog.show();
    }

AlertDialog.Builder에는 AlertDialog에 생성에 필요한 API를 제공하고 있다. setPositiveButton과 setNegativeButton을 이용하여 OK/Cancle 버튼을 버튼을 만든다.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.actions, menu);
        return true;
    }

onCreateOptionsMenu 함수를 이용하게 되면 코드로 옵션 메뉴를 생성할 수 있다. 하지만 먼저 menu 리소스가 필요하다.

'IT > Android' 카테고리의 다른 글

리스트 뷰  (0) 2017.10.31
Popup Menu 생성  (0) 2017.10.23
Intent  (0) 2017.10.23
Action Bar  (0) 2017.10.23
Dialog  (0) 2017.10.23

+ Recent posts