android组件之PopupWindow

      在开发过程中可以使用PopupWindow实现弹出小窗口进行操作。

  Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:AlertDialog的位置固定,而PopupWindow的位置可以随意


效果图:

android组件之PopupWindow_第1张图片

  既然PopupWindow可以在界面上显示属于自己的界面层。那就必须有属于自己的布局文件


  
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择你要的状态:" />
    


       


        


        
    


    
  然后配置下main.xml文件用来显示主界面,只需添加个按钮用来弹出PopupWindow即可

  接下来编写activity  (PS:在安卓中我们通过findViewById实例化组件,通过LayoutInflater转换布局文件)

package com.example.popupwindowdemo;

import com.example.popupwindowdemo.R.id;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView text = null;
	private Button but = null;
	private View view = null;
	private PopupWindow pupWin = null;
	private RadioGroup group = null;
	private Button cancel = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_main);
		this.text = (TextView) this.findViewById(R.id.text);
		this.but = (Button) this.findViewById(R.id.submit);
		this.but.setOnClickListener(new OnClickListenerImpl());

	}

	private class OnClickListenerImpl implements OnClickListener {

		@Override
		public void onClick(View v) {
			LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
			MainActivity.this.view = inflater.inflate(R.layout.popup, null);
			MainActivity.this.pupWin = new PopupWindow(MainActivity.this.view,
					500, 400, true);
			// MainActivity.this.pupWin.dismiss();
			MainActivity.this.group = (RadioGroup) MainActivity.this.view
					.findViewById(R.id.change);// 获取布局中的GroupRadio 由于已经被转换了
												// 得从View中获取
			MainActivity.this.cancel = (Button) MainActivity.this.view
					.findViewById(R.id.cancel); // 获取Pop中的取消按钮
			MainActivity.this.cancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					MainActivity.this.pupWin.dismiss();// 取消POpupwindow的显示

				}
			});
			MainActivity.this.pupWin.showAtLocation(MainActivity.this.group,
					Gravity.CENTER, 0, 0);// 设置popupwindow的显示位置
			MainActivity.this.group
					.setOnCheckedChangeListener(new OnCheckedChangeListenerImpl());

		}

		// 添加GroupRadio的响应事件
		private class OnCheckedChangeListenerImpl implements
				OnCheckedChangeListener {

			@Override
			public void onCheckedChanged(RadioGroup group, int id) {
				RadioButton but = (RadioButton) MainActivity.this.view
						.findViewById(group.getCheckedRadioButtonId());//获取RadioGroup中的组件
				
				MainActivity.this.text.setText("你当前选中的状态是:"+but.getText().toString());
				MainActivity.this.pupWin.dismiss();// 取消POpupwindow的显示

				

			}
		}
	}

}


  



你可能感兴趣的:(android组件之PopupWindow)