高速收费一类二类:PopupWindow
来源:百度文库 编辑:九乡新闻网 时间:2024/07/13 22:43:09
PopupWindow 1. 定义布局 供PopupWindow使用 如:hello.xml
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/robot" />
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dip"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HelloPop!"
/>
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/robot" />
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dip"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HelloPop!"
/>
2. 通过LayoutInflater 得到hello.xml 的 View view
Java代码
view = this.getLayoutInflater().inflate(R.layout.hello, null);
view = this.getLayoutInflater().inflate(R.layout.hello, null);
3. 创建PopupWindow pop 使用上面布局文件view
Java代码
pop = new PopupWindow(view,500,200);
pop = new PopupWindow(view,500,200);
4. 弹出PopupWindow
* 定义布局文件:main.xml 包括一个Button
Java代码
android:orientation="vertical"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="pop demo!"
/>
android:orientation="vertical"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="pop demo!"
/>
* 弹出:有2种方式:一个是下拉方式 一个是指定位置
- 下拉:
Java代码
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.showAsDropDown(v);
}
});
- 指定位置:
Java代码
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);
}
});
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. 取消
Java代码
view.findViewById(R.id.helloButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
pop.dismiss();
}
});
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
Java代码
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/robot" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HelloPop!"
/>
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="40dip"/>
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/robot" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HelloPop!"
/>
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="40dip"/>
6.2. 得到Spinner的实例:spinner
Java代码
spinner = (Spinner)view.findViewById(R.id.spinner);
spinner = (Spinner)view.findViewById(R.id.spinner);
6.3. 绑定Spinner与具体数据 本例以联系人为例
Java代码
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);
}
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);
}
写道
别忘了联系人访问权限:
6.4. 具体item的获取:
Java代码
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
}
});
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);
}
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/robot" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dip"
>
android:layout_height="wrap_content"
android:text="HelloPop!"
/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/robot" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dip"
>
android:layout_height="wrap_content"
android:text="HelloPop!"
/>
2. 通过LayoutInflater 得到hello.xml 的 View view
Java代码
view = this.getLayoutInflater().inflate(R.layout.hello, null);
view = this.getLayoutInflater().inflate(R.layout.hello, null);
3. 创建PopupWindow pop 使用上面布局文件view
Java代码
pop = new PopupWindow(view,500,200);
pop = new PopupWindow(view,500,200);
4. 弹出PopupWindow
* 定义布局文件:main.xml 包括一个Button
Java代码
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_height="wrap_content"
android:text="pop demo!"
/>
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_height="wrap_content"
android:text="pop demo!"
/>
* 弹出:有2种方式:一个是下拉方式 一个是指定位置
- 下拉:
Java代码
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.showAsDropDown(v);
}
});
- 指定位置:
Java代码
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);
}
});
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. 取消
Java代码
view.findViewById(R.id.helloButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
pop.dismiss();
}
});
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
Java代码
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/robot" />
android:layout_height="wrap_content"
android:text="HelloPop!"
/>
android:layout_width="wrap_content"
android:layout_height="40dip"/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/robot" />
android:layout_height="wrap_content"
android:text="HelloPop!"
/>
android:layout_width="wrap_content"
android:layout_height="40dip"/>
6.2. 得到Spinner的实例:spinner
Java代码
spinner = (Spinner)view.findViewById(R.id.spinner);
spinner = (Spinner)view.findViewById(R.id.spinner);
6.3. 绑定Spinner与具体数据 本例以联系人为例
Java代码
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);
}
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);
}
写道
别忘了联系人访问权限:
6.4. 具体item的获取:
Java代码
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
}
});
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);
}