android popupwindow 弹出窗口按钮事件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android popupwindow 弹出窗口按钮事件相关的知识,希望对你有一定的参考价值。
public void poplooksee(View v) TextView tv = (TextView) view.findViewById(R.id.poptxtobjectid); String objectID = tv.getText().toString(); TextView tvrs = (TextView) view.findViewById(R.id.poprstype); String rstype = tvrs.getText().toString(); if (rstype.equalsIgnoreCase(Constants.MSELL)) Intent intent = new Intent(); intent.putExtra("objectId", objectID); intent.setClass(ManagerActivity.this, MHouseDetailActivity.class); view.getContext().startActivity(intent); finish(); overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out); else Intent intent = new Intent(); intent.putExtra("objectId", objectID); intent.setClass(ManagerActivity.this, MRentHouseDetailActivity.class); view.getContext().startActivity(intent); finish(); overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out); 这是popupwindow中按钮事件的方法,但是当弹出窗口的时候单击该按钮,总是报错
这个view不能写在这里,除非你调用这个方法传入的是MainActivity这种继承Activity的Viewview.getContext().startActivity(intent); 参考技术A 报了什么错误 参考技术B 错误贴出来看看。你的代码看的眼花
Android 中PopupWindow弹出式窗口的使用
效果图如下:
实现代码如下:
activity_popup_window.xml按钮
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".PopupWindowActivity">
<Button
android:id="@+id/btn_popupWindow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="PopupWindow" />
</LinearLayout>
自定义弹出的视图layout_pop.xml,也可以用RecycleView或者ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_good"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text="好"
android:textColor="@color/gray"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/gray" />
<TextView
android:id="@+id/tv_not_too_bad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text="还行"
android:textColor="@color/gray"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/gray" />
<TextView
android:id="@+id/tv_bad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text="不好"
android:textColor="@color/gray"
android:textSize="20sp" />
</LinearLayout>
PopupWindowActivity类实现代码如下:
public class PopupWindowActivity extends AppCompatActivity
private Button btn_popupWindow;
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_window);
btn_popupWindow = findViewById(R.id.btn_popupWindow);
btn_popupWindow.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
View popup_view = LayoutInflater.from(PopupWindowActivity.this).inflate(R.layout.layout_pop, null);
TextView textView = popup_view.findViewById(R.id.tv_good);
textView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
popupWindow.dismiss();
Toast.makeText(PopupWindowActivity.this, "好", Toast.LENGTH_SHORT).show();
);
popupWindow = new PopupWindow(popup_view, btn_popupWindow.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
//设置弹出窗口应该接收外部触摸事件
popupWindow.setOutsideTouchable(true);
//设置可聚焦
popupWindow.setFocusable(true);
popupWindow.showAsDropDown(btn_popupWindow);
);
以上就是PopupWindow弹出式窗口的简单使用~
以上是关于android popupwindow 弹出窗口按钮事件的主要内容,如果未能解决你的问题,请参考以下文章
android中PopupWindow弹出窗体后,为啥不能点击其他控件