PopupWindow 使用及延伸

本文详细介绍如何使用PopupWindow组件实现轻量级的用户提醒功能。包括布局定义、弹出方式及位置控制等步骤,并演示了如何在PopupWindow内使用Spinner进行数据绑定及监听选择事件。

PopupWindow

 

[功能]

PopupWindow 作为一种用户提醒 而且其开销也比Activity要小

 

 

[代码 步骤]

1. 定义布局 供PopupWindow使用 如:hello.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    >
<ImageView  
	android:id="@+id/image"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/robot" />
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="20dip"
    >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="HelloPop!"
    />
<Button  
	android:id="@+id/helloButton"
    android:layout_width="100dip" 
    android:layout_height="wrap_content" 
    android:text="OK"
    />
 </LinearLayout>
</LinearLayout>

 

 

2. 通过LayoutInflater 得到hello.xml 的 View view

view = this.getLayoutInflater().inflate(R.layout.hello, null);

 

3. 创建PopupWindow pop 使用上面布局文件view

pop = new PopupWindow(view,500,200);

 

4. 弹出PopupWindow

* 定义布局文件:main.xml 包括一个Button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="pop demo!"
    />
<Button  
	android:id="@+id/button"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="to pop!"
    />
</LinearLayout>

 

 

* 弹出:有2种方式:一个是下拉方式 一个是指定位置

- 下拉:

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                    // TODO Auto-generated method stub
            	pop.showAsDropDown(v);
            }
  
    });

 

 

- 指定位置:

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                    // TODO Auto-generated method stub
            	pop.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 20, 20);
            	
            }
  
    });

 

 

5. 取消

view.findViewById(R.id.helloButton).setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                    // TODO Auto-generated method stub
            	pop.dismiss();
            	
            }
  
    });

 

6. 其他问题:

* 发现很多人对PopupWindow 里面包含ListView后 对具体哪个item被点击的获取有疑问 所以就顺便测试一下 发现和普通用法一样啊 没什么特别之处啊 现在把用法和大家分享分享

 

写道
因为ListView是展开显示的 会导致不美观 所以以Spinner为例

 

 

6.1. 定义包含Spinner 的布局文件 hello.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"
    >
<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
<ImageView  
	android:id="@+id/image"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/robot" />
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="HelloPop!"
    />
</LinearLayout>
<Spinner 
        android:id="@+id/spinner" 
        android:layout_width="wrap_content" 
        android:layout_height="40dip"/>
</LinearLayout>

 

6.2. 得到Spinner的实例:spinner

spinner = (Spinner)view.findViewById(R.id.spinner);

 

6.3. 绑定Spinner与具体数据 本例以联系人为例

public void specifySpinner(){
    	Cursor c = getContentResolver().query(People.CONTENT_URI, 
                null, null, null, null);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1,c, 
                new String[] {People.NAME}, 
                new int[] {android.R.id.text1});
        adapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
        
        
        spinner.setAdapter(adapter);
    }

 

写道
别忘了联系人访问权限:

<uses-permission android:name="android.permission.READ_CONTACTS" />

 

6.4. 具体item的获取:

spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

            public void onItemSelected(AdapterView<?> adapter,View v,
                    int pos, long id) {
            	updateTitle(pos);
            }

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}

        });

 

写道
updateTitle(int) 用来把位置在标题中显示

public void updateTitle(int i){
this.setTitle("HelloPop:"+i);
}

 

 

6.5. emulator 运行截图:

 

 

DSP(数信号处理器)的CAN(控制器局域网)通信中,按字节存储数据可通过以下方法实现。 ### 寄存器操作 CAN控制器通常有一系列寄存器用于存储和传输数据。在DSP中,可通过访问这些寄存器按字节存储数据。例如,某些CAN控制器有数据寄存器(如CANMSGIDx、CANMSGDLTx等),通过对这些寄存器的操作,能够将数据字节写入。以下是一个简单的伪代码示例: ```c // 假设CAN数据寄存器基地址为CAN_DATA_REG_BASE // 要存储的数据 unsigned char data[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; for(int i = 0; i < 8; i++) { *(CAN_DATA_REG_BASE + i) = data[i]; // 按字节存储数据到CAN数据寄存器 } ``` ### 内存映射 将CAN数据存储区域映射到DSP的内存空间,这样就能像操作普通内存一样按字节存储数据。例如,使用内存映射的方式将CAN接收缓冲区映射到特定的内存地址,然后直接对该内存地址进行操作。以下是一个简单的示例: ```c // 假设CAN接收缓冲区映射到内存地址CAN_RX_BUFFER_BASE // 要存储的数据 unsigned char data[8] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}; for(int i = 0; i < 8; i++) { *((unsigned char *)(CAN_RX_BUFFER_BASE + i)) = data[i]; // 按字节存储数据到CAN接收缓冲区 } ``` ### 数据帧处理 CAN通信以数据帧的形式传输数据,每个数据帧包含多个字节数据。在DSP中,可通过解析和处理这些数据帧来按字节存储数据。例如,当接收到一个CAN数据帧时,将数据帧中的每个字节提取出来并存储到相应的内存置。以下是一个简单的示例: ```c // 假设接收到的CAN数据帧存储在can_frame结构体中 typedef struct { unsigned char data[8]; } CAN_FRAME; CAN_FRAME can_frame; // 假设CAN数据帧已经接收到并存储在can_frame中 // 按字节存储数据到另一个缓冲区 unsigned char buffer[8]; for(int i = 0; i < 8; i++) { buffer[i] = can_frame.data[i]; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值