如何在alertDialog

时间:2018-02-04 15:45:03

标签: java android android-layout

我有一个带有颜色的alertDialog,它是从我的活动中的一个按钮调用的,我需要设置一个监听器来知道用户选择哪种颜色

private void colorDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.dialog_custom,null);
    builder.setView(dialogView);
    // Create and show the AlertDialog
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

dialog_custom.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <View
        android:id="@+id/red_circle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/circle_red"
        android:layout_margin="5dp"
        android:layout_toStartOf="@+id/blue_circle"/>
    <View
        android:id="@+id/blue_circle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="5dp"
        android:layout_toStartOf="@+id/yellow_circle"
        android:background="@drawable/circle_blue" />
    <View
        android:id="@+id/yellow_circle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/circle_yellow"
        android:layout_margin="5dp"
        android:layout_centerHorizontal="true"/>
    <View
        android:id="@+id/orange_circle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/circle_orange"
        android:layout_margin="5dp"
        android:layout_toEndOf="@id/yellow_circle"/>
    <View
        android:id="@+id/green_circle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/circle_green"
        android:layout_margin="5dp"
        android:layout_toEndOf="@id/orange_circle"/>

</RelativeLayout>

我知道我需要一个While循环来跟踪每种颜色,但我不知道如何将clickListener设置为我的alertDialog

感谢

4 个答案:

答案 0 :(得分:0)

这很简单

private void colorDialog(){
                AlertDialog.Builder builder = new AlertDialog.Builder(this);

                LayoutInflater inflater = getLayoutInflater();
                View dialogView = inflater.inflate(R.layout.dialog_custom,null);
                dialogView.findViewById(R.id.yourButton).setOnClickListener(new View.OnClickListener() {
                });
                builder.setView(dialogView);
                // Create and show the AlertDialog
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }

答案 1 :(得分:0)

 view.setOnClickListener(new 
       View.OnClickListener() {

         @Override
        public void onClick(View v) {
         switch(v.getId()){
     case R.id.blue_circle : //do sth 
          break;
     case R.id.orange_circle : //do sth 
          break;
     case R.id.green_circle : //do sth 
          break;
     case R.id.red_circle : //do sth 
          break;
     case R.id.yellow_circle : //do sth 
          break;

}
    }

答案 2 :(得分:0)

在您的情况下执行此操作的正确方法是,实现如下: - 在您的活动中编写如下函数

public void onClickOnColor(View view){

            switch(view.getId()) {
                case R.id.red_circle:
                    Toast.makeText(LoginActivity.this,"Red",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.yellow_circle:
                    Toast.makeText(LoginActivity.this,"Yellow",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.blue_circle:
                    Toast.makeText(LoginActivity.this,"Blue",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.orange_circle:
                    Toast.makeText(LoginActivity.this,"Orange",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.green_circle:
                    Toast.makeText(LoginActivity.this,"Green",Toast.LENGTH_SHORT).show();
                    break;

            }
    }

并在dialog_custom.xml中为每个视图分配点击监听器 android:onClick =&#34; onClickOnColor&#34; 请参阅以下内容: -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <View
        android:id="@+id/red_circle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/circle_red"
        android:layout_margin="5dp"
        **android:onClick="onClickOnColor"**
        android:layout_toStartOf="@+id/blue_circle"/>
<View
        android:id="@+id/blue_circle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="5dp"
        android:layout_toStartOf="@+id/yellow_circle"
   **android:onClick="onClickOnColor"**
        android:background="@drawable/circle_blue" />
    <View
        android:id="@+id/yellow_circle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/circle_yellow"
        android:layout_margin="5dp"
   **android:onClick="onClickOnColor"**
        android:layout_centerHorizontal="true"/>
    <View
        android:id="@+id/orange_circle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/circle_orange"
        android:layout_margin="5dp"
   **android:onClick="onClickOnColor"**
        android:layout_toEndOf="@id/yellow_circle"/>
    <View
        android:id="@+id/green_circle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        **android:background="@drawable/circle_green"**
        android:layout_margin="5dp"
   **android:onClick="onClickOnColor"**
        android:layout_toEndOf="@id/orange_circle"/>

</RelativeLayout>

答案 3 :(得分:0)

构建dialogView后:

dialogView.findViewById(R.id.red_circle).setOnClickListener(this);
dialogView.findViewById(R.id.blue_circle).setOnClickListener(this);
dialogView.findViewById(R.id.yellow_circle).setOnClickListener(this);
dialogView.findViewById(R.id.orange_circle).setOnClickListener(this);
dialogView.findViewById(R.id.green_circle).setOnClickListener(this);

在您的类实现View.OnClickListener:

之后
    @Override
public void onClick(View view) {

    switch (view.getId()) {
        case R.id.red_circle:
            Toast.makeText(this, "red", Toast.LENGTH_SHORT).show();
            break;

        case R.id.blue_circle:
            Toast.makeText(this, "blue", Toast.LENGTH_SHORT).show();
            break;

        case R.id.yellow_circle:
            Toast.makeText(this, "yellow", Toast.LENGTH_SHORT).show();
            break;

        case R.id.orange_circle:
            Toast.makeText(this, "orange", Toast.LENGTH_SHORT).show();
            break;

        case R.id.green_circle:
            Toast.makeText(this, "green", Toast.LENGTH_SHORT).show();
            break;
    }
}