一、出现的问题
- 最近在看明日科技的书籍《安卓精彩编程200例》,第一个例子就是用PopupWindow模仿微信的右上角菜单,如下图所示:
- 但是书中也就给出这么一个效果而已,没有说怎么给菜单中的按钮绑定单击事件。按钮若是不能响应单击事件,那么要这些按钮有何用?
- 一开始我直接用findViewById()去查找PopupWindow中的按钮,然后为它绑定单击事件。但是结果出现了空指针异常,程序闪退了……
二、解决方法
1.上面说用findViewById()其实没有错,但是不能直接用,需要像下面这样用:
popupWindow_view.findViewById(R.id.chat);
也就是在findViewById(R.id.chat)这个方法前面,要加上PopupWindow的视图对象。直接用findViewById(R.id.chat)的话,那么就是从主布局里面去找按钮,显然主布局里面并没有PopupWindow的按钮,肯定会找不到,于是会出现空指针异常。
三、代码
1. java代码
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
private Button btn;
private PopupWindow popupWindow;
private Button chatBtn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//PopupWindow的视图对象
View view = getLayoutInflater().inflate(R.layout.menu, null, false);
popupWindow=new PopupWindow(view, ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.WRAP_CONTENT,true);
popupWindow.showAsDropDown(btn,0,0);
//下面这句代码是关键,通过popupwindow的视图对象去调用findViewById()
chatBtn=view.findViewById(R.id.chat);
chatBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(MainActivity.this,"鲁班大师,智商二百五",Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
2. 布局代码(主布局activity_main.xml+菜单布局menu.xml)
先是主布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/bg"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信"
android:textSize="25sp"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:background="@drawable/btn_add"/>
</RelativeLayout>
下面是菜单布局的代码,也就是popupwindow对应的布局代码,文件名是menu.xml,和activity_main.xml放在同一个目录下。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="right"
android:layout_height="match_parent">
<Button
android:id="@+id/chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333333"
android:textColor="#ffffff"
android:drawableLeft="@drawable/icon_group"
android:drawablePadding="10dip"
android:textSize="18sp"
android:paddingRight="18dp"
android:layout_weight="1"
android:text="发起群聊"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333333"
android:textColor="#ffffff"
android:drawableLeft="@drawable/btn_add"
android:drawablePadding="10dip"
android:textSize="18sp"
android:paddingRight="18dp"
android:layout_weight="1"
android:text="添加好友"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333333"
android:textColor="#ffffff"
android:drawableLeft="@drawable/icon_scan"
android:drawablePadding="10dip"
android:textSize="18sp"
android:paddingRight="36dp"
android:layout_weight="1"
android:text="扫一扫"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333333"
android:textColor="#ffffff"
android:drawableLeft="@drawable/icon_card"
android:drawablePadding="10dip"
android:textSize="18sp"
android:layout_weight="1"
android:paddingRight="36dp"
android:text="收付款"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333333"
android:textColor="#ffffff"
android:drawableLeft="@drawable/icon_help"
android:drawablePadding="10dip"
android:textSize="18sp"
android:layout_weight="1"
android:text="帮助与反馈"/>
</LinearLayout>
四、效果
我只给“发起群聊”这个按钮绑定单击事件,效果如下图,弹出“鲁班大师,智商二百五”。