Android学习之PopupWindow弹出窗口

阅读更多

main.xml:



	
	

 

 

 

popwindow.xml:



	
	
		
		
		
	
	

 

 

 

MyPopupWindowDemo.java:

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
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 MyPopupWindowDemo extends Activity {

	private Button popbut = null; 							// 按钮组件
	private RadioGroup changestatus = null; 				// 单选钮组件
	private TextView statusinfo = null; 					// 文本显示组件
	private Button cancel = null; 							// 按钮组件
	private PopupWindow popWin = null; 						// 弹出窗口
	private View popView = null; 							// 保存弹出窗口布局

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main); 				// 设置布局管理器
		this.popbut = (Button) super.findViewById(R.id.popbut); // 取得组件
		this.statusinfo = (TextView) super.findViewById(R.id.statusinfo); // 取得组件
		this.popbut.setOnClickListener(new OnClickListenerImpl()); // 设置单击事件
	}

	private class OnClickListenerImpl implements OnClickListener {	// 设置监听
		@Override
		public void onClick(View view) {
			LayoutInflater inflater = LayoutInflater
					.from(MyPopupWindowDemo.this);			// 取得LayoutInflater对象
			MyPopupWindowDemo.this.popView = inflater.inflate(
					R.layout.popwindow, null);				// 读取布局管理器
			MyPopupWindowDemo.this.popWin = new PopupWindow(popView, 300, 220,
					true);									// 实例化PopupWindow
			MyPopupWindowDemo.this.changestatus = (RadioGroup) popView
					.findViewById(R.id.changestatus); 		// 取得组件
			MyPopupWindowDemo.this.cancel = (Button) popView
					.findViewById(R.id.cancel); 			// 取得组件
			MyPopupWindowDemo.this.changestatus
					.setOnCheckedChangeListener(
							new OnCheckedChangeListenerImpl());	// 设置监听
			MyPopupWindowDemo.this.cancel
					.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							MyPopupWindowDemo.this.popWin.dismiss(); // 关闭弹出窗口
						}
					});
			MyPopupWindowDemo.this.popWin.showAtLocation(
					MyPopupWindowDemo.this.popbut, 
					Gravity.CENTER, 0, 0);					// 显示弹出窗口
		}
	}

	private class OnCheckedChangeListenerImpl implements
			OnCheckedChangeListener {						// 选项选中时触发

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			RadioButton but = (RadioButton) MyPopupWindowDemo.this.popView
					.findViewById(group.getCheckedRadioButtonId());	// 取得选中组件
			MyPopupWindowDemo.this.statusinfo.setText("当前用户状态:"
					+ but.getText().toString());			// 设置文本
			MyPopupWindowDemo.this.popWin.dismiss(); 		// 关闭弹出窗口
		}

	}
}

 

你可能感兴趣的:(PopupWindow,弹出窗口)