Java onclick事件打开新弹出窗口

Java onclick事件打开新弹出窗口,java,android,xml,Java,Android,Xml,我有一个MainActivity.xml,其中我有ExpandableListView,在ExpandableListView中我有组列表,每个组都有一些项目,在我有按钮的项目后面,我需要在按钮上创建onclick事件,我需要单击按钮,然后打开一个小弹出窗口,我用弹出窗口创建了一个xml文件,如何创建onclick事件,我尝试了一周,但没有任何效果。我必须在其中创建onclick ActivityMain.xml-是可扩展的列表视图 <TextView android:id=

我有一个MainActivity.xml,其中我有ExpandableListView,在ExpandableListView中我有组列表,每个组都有一些项目,在我有按钮的项目后面,我需要在按钮上创建onclick事件,我需要单击按钮,然后打开一个小弹出窗口,我用弹出窗口创建了一个xml文件,如何创建onclick事件,我尝试了一周,但没有任何效果。我必须在其中创建onclick

  • ActivityMain.xml-是可扩展的列表视图

     <TextView
         android:id="@+id/Header"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="@color/colorACD_gray"
         android:fontFamily="@font/sourcesanspro_bold"
         android:padding="10dp"
         android:text="@string/acd_keyconfig"
         android:textAlignment="viewStart"
         android:textColor="@color/white"
         android:textSize="20sp"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
    
     <LinearLayout
         android:id="@+id/line"
         android:layout_width="wrap_content"
         android:layout_height="5dp"
         android:layout_below="@id/Header"
         android:layout_alignLeft="@+id/Header"
         android:layout_alignRight="@+id/Header"
         android:background="@color/colorACD_orange"
         android:orientation="horizontal" />
    
     <ExpandableListView
         android:id="@+id/lvExp"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_below="@+id/line"
         android:layout_alignParentBottom="false"
         android:divider="@color/colorACD_orange"
         android:dividerHeight="2dp"
         android:groupIndicator="@drawable/settings_selector" />
    

    
    
  • List_Item.xml-有一个按钮

     <?xml version="1.0" encoding="utf-8"?>
    
    
    

    
    
-ExpandableListAdapter.java
```包com.example.test;
导入java.util.HashMap;
导入java.util.List;
导入android.content.Context;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseExpandableListAdapter;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私人语境(private Context)(私人语境);;
私有列表_listDataHeader;//头标题
//标题标题、子标题格式的子数据
私有HashMap_listDataChild;
公共ExpandableListAdapter(上下文、列表listDataHeader、,
HashMap listChildData){
这._context=context;
这。_listDataHeader=listDataHeader;
这。_listDataChild=listChildData;
}
@凌驾
公共对象getChild(int-groupPosition、int-ChildPosition){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.get(childpositionon);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(int groupPosition,final int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
最终字符串childText=(字符串)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_项,空);
}
TextView txtListChild=(TextView)convertView.findViewById(R.id.lblListItem);
setText(childText);
返回视图;
}
@凌驾
公共视图getGroupView(int-groupPosition,布尔值isExpanded,
视图(视图、视图组父级){
字符串头文件=(字符串)getGroup(groupPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_组,空);
}
TextView lblistheader=(TextView)convertView
.findviewbyd(R.id.lblistheader);
lblListHeader.setTypeface(空);
lblListHeader.setText(标题);
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回此。\u listDataHeader.get(groupPosition);
}
@凌驾
public int getGroupCount(){
返回此值。_listDataHeader.size();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
}
```

您需要在
getChildView()方法中创建
onClick
事件。就像您为您的孩子使用
setText()
一样
TextView
您也可以将其用于按钮。像这样:

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);


    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
    txtListChild.setText(childText);

    Button button = convertView.findViewById(R.id.button1);
    button.setOnClickListener(); <-------------- HERE
    return convertView;
}
现在,当您拥有这个自定义弹出对话框时,您可以在
onClick方法中这样调用它:

Button button = convertView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupDialog popup_dialog = new PopupDialog (_context);
                popup_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                popup_dialog.setContentView(R.layout.popup_dialog_layout);
                Window help_window = popup_dialog.getWindow();
                help_window.setLayout(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
                popup_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                popup_dialog.setCanceledOnTouchOutside(false);
                popup_dialog.show();
            }
        });
并将其添加到
主题.xml
样式.xml

<!-- Dialog Theme -->
<style name="NoActionBarDialog" parent="Theme.MaterialComponents.Light.Dialog.Bridge">
    <item name="windowActionBar">false</item>
    <item name="android:windowMinWidthMajor">97%</item>
    <item name="android:windowMinWidthMinor">97%</item>
    <item name="windowNoTitle">true</item>
    <item name="android:colorBackground">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowActionBarOverlay">false</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
</style>

假的
97%
97%
真的
@android:彩色/透明
@android:彩色/透明
假的
假的

这可能会对您有所帮助:好的,我在代码中添加了这两行,但是现在,我需要用打开新窗口的代码创建public void,或者?对不起,我是初学者。这取决于你如何创建弹出窗口。你能给我看看你弹出窗口的代码吗?我将在完成显示过程后编辑弹出窗口,我将为输入文本、按钮添加一些内容,要做到这一点,您需要创建一个类来扩展对话框和内部处理按钮单击以及其他所有内容。我将编辑我的答案,给你举个例子。
public class PopupDialog extends Dialog {

    private Context context;

    public PopupDialog(@NonNull Context context) {
        super(context, R.style.NoActionBarDialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.YOUR_POPUP_LAYOUT);

        TextView text = findViewById(R.id.popup_text);
        //find your views like this and use them as you want

    }
    
    @Override
    public void onBackPressed() {
        //do nothing <----- only this if you want to forbid the user to exit the dialog with the back button, else don't override this method
    }
}
Button button = convertView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupDialog popup_dialog = new PopupDialog (_context);
                popup_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                popup_dialog.setContentView(R.layout.popup_dialog_layout);
                Window help_window = popup_dialog.getWindow();
                help_window.setLayout(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
                popup_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                popup_dialog.setCanceledOnTouchOutside(false);
                popup_dialog.show();
            }
        });
<!-- Dialog Theme -->
<style name="NoActionBarDialog" parent="Theme.MaterialComponents.Light.Dialog.Bridge">
    <item name="windowActionBar">false</item>
    <item name="android:windowMinWidthMajor">97%</item>
    <item name="android:windowMinWidthMinor">97%</item>
    <item name="windowNoTitle">true</item>
    <item name="android:colorBackground">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowActionBarOverlay">false</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
</style>