main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/statusinfo"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="当前用户状态:在线" />
<Button
android:id="@+id/popbut"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="状态" />
</LinearLayout>
popwindow.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/popinfo"
android:text="请选择您的当前状态:"
android:layout_width="wrap_content"
android:textSize="20px"
android:layout_height="wrap_content"/>
<RadioGroup
android:id="@+id/changestatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/online">
<RadioButton
android:id="@+id/online"
android:text="在线"/>
<RadioButton
android:id="@+id/offline"
android:text="离线"/>
<RadioButton
android:id="@+id/stealth"
android:text="隐身"/>
</RadioGroup>
<Button
android:id="@+id/cancel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="取消" />
</LinearLayout>
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(); // 关闭弹出窗口
}
}
}