Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android自定義彈窗效果

Android自定義彈窗效果

編輯:關於Android編程

Android的彈窗效果有很多種,就最簡單而言,就可以調用一個AlertDialog彈窗顯示,可是要自定義彈窗效果有以下這種方法,就我個人而言感覺挺方便的,適用性也挺廣的。

 

首先先簡單寫個AlertDialog的使用

 

public void showDialog(){
			AlertDialog dialog = new AlertDialog.Builder(this)
			.setTitle(提示)
			.setMessage(getResources().getString(你確定退出嗎?)
			.setPositiveButton(取消, new OnClickListener() {
			
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			}).setNegativeButton(確定, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			}).create();
			dialog.show();	
		}	

	}

 

 

接下來就是自定義彈窗效果圖:

\

 

\

 

 

 

下面是示例代碼:

 



    
        


 

 

public class SelectPopupWindow extends PopupWindow {
	Button takePhoto;
	Button selectFromAlbum;
	Button cancel;
	View menuView;
	public SelectPopupWindow(Context ctx, OnClickListener onClickListener) {
		LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		menuView = inflater.inflate(R.layout.modify_head, null); //可以采用各種樣式,自定義view
		takePhoto = (Button) menuView.findViewById(R.id.takephoto);
		takePhoto.setOnClickListener(onClickListener);
		selectFromAlbum = (Button) menuView.findViewById(R.id.selectfromalbum);
		selectFromAlbum.setOnClickListener(onClickListener);		
		cancel = (Button) menuView.findViewById(R.id.cancel);
		cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});
		
		setContentView(menuView);
		setWidth(LayoutParams.MATCH_PARENT);
		setHeight(LayoutParams.WRAP_CONTENT);
		setFocusable(true);   //彈出窗體後可點擊
		
		//setAnimationStyle(R.style.AnimBottom);   //設置動畫
		menuView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int height = menuView.findViewById(R.id.top).getTop();
				Log.e(huang,  top height:+height+ y:+event.getY());
				int y = (int) event.getY();
				if(y

 

 

public class TestPopupWindow extends Activity {
	private SelectPopupWindow popupWindow;
	private ImageView image;
	private Button btn, btn1;
	private Uri fileUri;     
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main1);
		init();
	}
	
	private void init(){
		image = (ImageView) findViewById(R.id.image);
		btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(onClickListener);
		btn1 = (Button) findViewById(R.id.btn1);
		btn1.setOnClickListener(onClickListener);
		
	}
	
	View.OnClickListener onClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.btn:
				test();
				break;
			case R.id.btn1:
				startActivity(new Intent(TestPopupWindow.this, TestGsonActivity.class));
				break;
			case R.id.takephoto:
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
				startActivityForResult(intent, 100);
				break;
			case R.id.selectfromalbum:
				Intent intent1 = new Intent(Intent.ACTION_PICK);
				intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image/*);
				startActivityForResult(intent1, 200);
				break;
				
			}
		}
	};
	
	public void test(){
		popupWindow = new SelectPopupWindow(this, onClickListener);
		popupWindow.showAtLocation(findViewById(R.id.main), 
				Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
	}
	


 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved