android 弹出窗口组件(PopupWindow)

android 弹出窗口组件(PopupWindow)_第1张图片

 

 

既然PopupWindow组件可以在界面上显示一个自己的界面层,那就需要一个专门的布局文件。

  只要是组件就一定离不开布局文件。

  既然后面要出现的布局是通过PopupWindow文件配置的,那么所有组件必须使用一个转换类。

 

 

 

在main.xml中

"1.0" encoding="utf-8"?>

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:gravity="center_horizontal">

 

     android:id="@+id/statusinfo"

     android:layout_marginTop="8dp"

     android:layout_width="fill_parent"

     android:layout_height="wrap_content"

     android:gravity="center_horizontal"

     android:text="请选择您喜欢的三国人物" />

 

     android:id="@+id/popbut"

     android:layout_marginTop="8dp"

     android:layout_width="60dp"

     android:layout_height="wrap_content"

     android:text="选择" />

 

 

 

 

新建布局文件popwindow.xml

"1.0" encoding="utf-8"?>

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:gravity="center_horizontal">

 

 

     android:id="@+id/choose"

     android:layout_marginTop="100dp"

     android:layout_width="wrap_content"

     android:layout_height="wrap_content"

     android:orientation="vertical">

    

       android:id="@+id/simayi"

       android:text="司马懿" />

    

       android:id="@+id/zhugeliang"

       android:text="诸葛亮" />

    

       android:id="@+id/zhouyu"

       android:text="周瑜" />

    

         android:id="@+id/xuyou"

         android:text="许攸"/>

 

 

     android:id="@+id/cancel"

     android:layout_width="60dp"

     android:layout_height="wrap_content"

     android:text="取消"/>

 

 

 

 

在MyPopupWindowDemo.java程序中

package com.li.popupwindow;

 

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 TextView statusinfo = null;

  private View popView = null ;

  private PopupWindow popWin = null ;

  private RadioGroup changestatus = null ;

  private Button cancel = 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 {

 

     public void onClick(View v) {

       LayoutInflater inflater = LayoutInflater.from(MyPopupWindowDemo.this) ;

       MyPopupWindowDemo.this.popView = inflater.inflate(

            R.layout.popwindow, null);  // 找到了布局文件中的View

       MyPopupWindowDemo.this.popWin = new PopupWindow(

            MyPopupWindowDemo.this.popView, 300,600, true);

       MyPopupWindowDemo.this.changestatus = (RadioGroup) MyPopupWindowDemo.this.popView

            .findViewById(R.id.choose); // 取得弹出界面中的组件

       MyPopupWindowDemo.this.cancel = (Button) MyPopupWindowDemo.this.popView

            .findViewById(R.id.cancel);

       MyPopupWindowDemo.this.changestatus

            .setOnCheckedChangeListener(new OnCheckedChangeListenerImpl());

       MyPopupWindowDemo.this.cancel.setOnClickListener(new OnClickListener(){

 

         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 {

 

    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() ;

     }

    

  }

}

 



 

你可能感兴趣的:(Android应用)