Android 는 사용자 정의 alertdialog 를 사용 하여 종료 확인 단 추 를 실행 합 니 다.

때때로 우 리 는 게임 이나 응용 프로그램 에서 우리 스타일 에 맞 는 알림 상자(AlertDialog)를 사용 해 야 한다.다음은 내 가 작은 게임 을 개발 하 는 과정 에서 정리 한 것 이다.여러분 에 게 유용 하 기 를 바란다.
선행 효과 도:

다음은 배경 그림 이나 단 추 를 사용 한 그림 입 니 다.
자 료 를 찾 고 예 를 참고 한 후에 야 이러한 효 과 를 실현 하 는 것 이 매우 간단 하 다 는 것 을 알 게 되 었 다.바로 alertDialog 의 contentView 를 설정 하 는 것 이다.
다음 코드 는 Activity 에 적 혀 있 습 니 다.코드 는 다음 과 같 습 니 다.

public boolean onKeyDown(int keyCode, KeyEvent event) {
//       ,       
if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
showExitGameAlert();
}
return super.onKeyDown(keyCode, event);
}
private void showExitGameAlert() {
final AlertDialog dlg = new AlertDialog.Builder(this).create();
dlg.show();
Window window = dlg.getWindow();
// ***               .
//          ,shrew_exit_dialog.xml     view  
window.setContentView(R.layout.shrew_exit_dialog);
//          ,        
ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
exitApp(); //     ...
}
});
//   alert    
ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dlg.cancel();
}
});
}    layout  ,             .     Activity   .
     : shrew_exit_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:Android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" 
android:layout_width="wrap_content">
<!--          -->
<ImageView android:id="@+id/exitGameBackground"
android:layout_centerInParent="true" 
android:layout_height="wrap_content"
android:layout_width="wrap_content" 
android:src="@drawable/bg_exit_game" />
<!--      -->
<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
android:layout_alignLeft="@+id/exitGameBackground"
android:layout_marginBottom="30dp" 
android:layout_marginLeft="35dp"
android:id="@+id/btn_ok" 
android:layout_height="wrap_content"
android:layout_width="wrap_content" 
android:background="@drawable/btn_ok" />
<!--      -->
<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
android:layout_alignRight="@+id/exitGameBackground"
android:layout_marginBottom="30dp" 
android:layout_marginRight="35dp"
android:id="@+id/btn_cancel" 
android:layout_height="wrap_content"
android:layout_width="wrap_content" 
android:background="@drawable/btn_cancel" />
</RelativeLayout>          ,        AlertDialog    .                   .
alertdialog 종료 확인 단추 인 스 턴 스 코드:

package com.example.alertdialog;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//     onBackPressed,             ,      。
public void onBackPressed1(View v) { 
new AlertDialog.Builder(this).setTitle("     ?") 
.setIcon(android.R.drawable.ic_dialog_info) 
.setPositiveButton("  ", new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
//   “  ”     
MainActivity.this.finish(); 
} 
}) 
.setNegativeButton("  ", new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
//   “  ”    ,            
Toast.makeText(MainActivity.this, "       ", Toast.LENGTH_LONG).show();
} 
}).show(); 
}
}

좋은 웹페이지 즐겨찾기