PopupWindow

在软件开发中,用户可以Popupwindow组件,在窗口上弹出一个小窗口进行一些操作,例如一些即时通讯软件,在更改状态时,可以弹出窗口进行操作。popupwindow布局需要内嵌在管理器中,即要用到inflater转换器

主程序MyDemo.java

package com.jackie.popwindowproject;
import android.app.Activity;
import android.os.Bundle;
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 MyDemo extends Activity {
private Button status=null;
private TextView show=null;
private View popView=null;
private PopupWindow popWin=null;
private RadioGroup radioBut=null;
private Button cancel=null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.show=(TextView) super.findViewById(R.id.show);
this.status=(Button) super.findViewById(R.id.status);
this.status.setOnClickListener(new OnClickListenerImpl());
}
    private class OnClickListenerImpl  implements OnClickListener{


@Override
public void onClick(View arg0) {
LayoutInflater inflater=LayoutInflater.from(MyDemo.this);
MyDemo.this.popView=inflater.inflate(R.layout.popwindow, null);//找到布局文件中的View
MyDemo.this.popWin=new PopupWindow(MyDemo.this.popView,600,500);
MyDemo.this.radioBut=(RadioGroup) MyDemo.this.popView.findViewById(R.id.radioBut);
MyDemo.this.cancel=(Button) MyDemo.this.popView.findViewById(R.id.cancel);
MyDemo.this.radioBut.setOnCheckedChangeListener(new OnCheckedChangeListenerImpl());
MyDemo.this.cancel.setOnClickListener(new OnClickListener(){


@Override
public void onClick(View arg0) {
MyDemo.this.popWin.dismiss();

}});
MyDemo.this.popWin.showAtLocation(popView, Gravity.CENTER, 20, 20);
}
   
    }
    private class OnCheckedChangeListenerImpl implements OnCheckedChangeListener{


@Override
public void onCheckedChanged(RadioGroup group, int arg1) {
RadioButton but=(RadioButton) MyDemo.this.popView.findViewById(group.getCheckedRadioButtonId());
MyDemo.this.show.setText("当前用户状态:"+but.getText().toString());
MyDemo.this.popWin.dismiss();

}
   
    }

主布局文件main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MyDemo" >
    <TextView
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="当前用户状态:" />
    <Button 
        android:id="@+id/status"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="状态"/>
</LinearLayout>

窗口布局文件PopupWindow.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MyDemo" >
    <TextView
        android:id="@+id/popinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择当前的状态:"
        android:textSize="40px" />


    <RadioGroup
        android:id="@+id/radioBut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checkedButton="@+id/online"
        android:orientation="vertical" >


        <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_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消"/>
</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值