[英]How to set a clicklistener to image in alertDialog
我有帶有顏色的 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
謝謝
就這么簡單
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();
}
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;
}
}
在您的情況下執行此操作的正確方法是,實現如下:- 在您的活動中編寫如下所示的函數
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="onClickOnColor"參見以下內容:-
<?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>
建立你的 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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.