0

이미지 버튼을 클릭하면 팝업 알림 팝업이 나타납니다. 버튼의 기본 모양을 사용하는 대신 "ok"및 "cancel"단추를 사용자 정의하려면 어떻게해야합니까? "Ok"및 "cancel"로 사용자 정의 ImageButton을 사용하고 싶습니다.팝업 알림을 맞춤 설정하는 방법은 무엇입니까?

다음은 팝업 알림을위한 코드입니다. 나는 "를 넣어 원하는 거기에"확인 "과 붉은 색에서"취소 "가되는 그래서 그 대신

enter image description here

: 여기

public class Notifications extends AppCompatActivity { 


ImageButton Notifications; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_notifications); 


    Notifications = (ImageButton) findViewById(R.id.AllowNotifications); 


Notifications.setOnClickListener(new View.OnClickListener(){ 
    @Override 
    public void onClick(View v) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(Notifications.this); 

     builder.setCancelable(false); //False= ONLY way to exist notification is by clicking NO 
     //True= Exit notification by clicking anywhere on screen outside of notification box. 

     builder.setTitle("Here is the alert dialog"); 
     builder.setMessage("Here is my message thing"); 

     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int WhichButton) { 
       dialog.cancel(); 
      } 
     }); 


     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

      } 
     }); 

     builder.show(); 

       } 
      }); 
} 
} 

은 기본적으로 위의 코드와 함께 알림 팝업 것 확인 "및"취소 "를 내 사용자 지정 이미지 단추로 사용하면 빨간색을 다른 것으로 변경하려고합니다. 팝업 알림에서 어떻게해야합니까?

답변

0

대화 상자의 모양을 모두 사용자 정의하고 단추, TextViews 등을 추가하려는 경우 - DialogFragment를 확장하고 View.OnClickListener를 구현하는 클래스를 만들어야하며 두 개의 탭으로 자신 만의 레이아웃을 만들어야합니다. 그것을 위해 주문 제작 버튼. https://developer.android.com/reference/android/app/DialogFragment.html

public static class MyDialogFragment extends DialogFragment implements View.OnClickListener { 
    static MyDialogFragment newInstance() { 
     return new MyDialogFragment(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.dialog_fragment, container, false); 
     v.findViewById(R.id.btn_ok).setOnClickListener(this); 
     return v; 
    } 

    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.btn_ok: 
       // do something 
       break; 
      default: 
       break; 
     } 
    } 
} 

그리고 당신의 활동에서 당신이 :에 입력으로 그들에게 ID와 설정 OnClickListeners

을 부여

void showDialog() { 
    // Create the fragment and show it as a dialog. 
    DialogFragment newFragment = MyDialogFragment.newInstance(); 
    newFragment.show(getFragmentManager(), "dialog"); 
} 
0

documentation says으로, 사용자 레이아웃 세션을 만들기 에, 당신은 사용자 정의 레이아웃을 생성하고 대화 상자에서 그것을 팽창시킬 수 있습니다.

AlertDialog.Builder에 의해 생성 된 단추와 다른 단추를 사용하려면 해당 단추의 클릭 수신기를 처리해야합니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical" 
    android:padding="20dp"> 

    <TextView 
     android:id="@+id/dialogTitle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Here is the alert dialog" 
     android:textColor="@android:color/black" 
     android:textSize="16sp" 
     android:textStyle="bold"/> 

    <TextView 
     android:id="@+id/dialogSubtitle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Here is my message thing"/> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="32dp"> 

     <Button 
      android:id="@+id/positiveButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:background="@android:color/transparent" 
      android:text="OK" 
      android:textColor="@android:color/holo_red_light"/> 

     <Button 
      android:id="@+id/negativeButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginEnd="40dp" 
      android:layout_toStartOf="@id/positiveButton" 
      android:background="@android:color/transparent" 
      android:text="Cancel" 
      android:textColor="@android:color/holo_red_light"/> 

    </RelativeLayout> 

</LinearLayout> 

그리고 만들 수있는 코드가 실행 : 이것은처럼 보이는 방법의 스크린 샷

LayoutInflater layoutInflater = LayoutInflater.from(this); 
     View promptView = layoutInflater.inflate(R.layout.test, null); 

     final AlertDialog alertD = new AlertDialog.Builder(this).create(); 

     TextView title = (TextView) promptView.findViewById(R.id.dialogTitle); 
     TextView subtitle = (TextView) promptView.findViewById(R.id.dialogSubtitle); 

     title.setText("My new Custom Dialog"); 
     subtitle.setText("With everything that I want"); 

     Button positive = (Button) promptView.findViewById(R.id.positiveButton); 
     Button negativeButton = (Button) promptView.findViewById(R.id.negativeButton); 

     positive.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // btnAdd1 has been clicked 
      } 
     }); 

     negativeButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // btnAdd2 has been clicked 
      } 
     }); 

     alertD.setView(promptView); 
     alertD.show(); 

입니다

이것은 내가 솔루션을 테스트하기 위해 만든 레이아웃입니다 내 휴대 전화. 요구 사항에 맞게 레이아웃을 자유롭게 변경할 수 있습니다.

Example of Dialog

this answers for other question에서 그것을 설명 크람 덕분에,하지만 난 당신의 질문에 대한 특정 코드가 더 좋을 것이라고 생각했다.

관련 문제