【问题标题】:Multiple buttons in a popup, trying to stay organized弹出窗口中有多个按钮,试图保持井井有条
【发布时间】:2015-01-20 08:23:36
【问题描述】:

我正在片段 Tab3Fragment 中实现一个弹出窗口,并希望保留弹出窗口代码,

 public void showPopup(View anchorView){
 } 

并尽可能地最小化 Tab3Fragment 中的其他地方以保持整洁。

目前 showPopup 看起来像这样,

public void showPopup(View anchorView) {    
    Button btnDismiss, btnFirstRecord, btnPreviousRecord, btnNextRecord, btnLastRecord;
    LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemServi (Context.LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.popup_layout, null);
    final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);    
    TextView tv = (TextView)popupView.findViewById(R.id.tv);
    tv.setText("Blah, blah, blah");

    btnDismiss = (Button)popupView.findViewById(R.id.btnDismissxml);
    btnFirstRecord = (Button)popupView.findViewById(R.id.btnFirstRecordxml);
    btnPreviousRecord = (Button)popupView.findViewById(R.id.btnPreviousRecordxml);
    btnNextRecord = (Button)popupView.findViewById(R.id.btnNextRecordxml);
    btnLastRecord = (Button)popupView.findViewById(R.id.btnLastRecordxml);

    popupWindow.setFocusable(true);
    int location[] = new int[2];
    anchorView.getLocationOnScreen(location);
    popupWindow.showAtLocation(anchorView, Gravity.CENTER, 0, 0);
    }   
}

问题: 有没有办法在 showPopup 中实现 onClick 方法 case-switch 语句来处理这个问题?也许像,

@Override
public void onClick(View v) {       
    switch (v.getId()) {
    case R.id.btnFirstRecordxml:
        //firstRecord(v);
        break;
    case R.id.btnPreviousRecordxml:
        //previousRecord(v);
        break;
    case R.id.btnNextRecordxml:
        //nextRecord(v);
        break;
    case R.id.btnLastRecordxml:
        //lastRecord(v);
        break;
    case R.id.btnDismissxml:
        //closePopup(v);
        break;  
    }}

其他解决方案比如把onClick放到popup_layout.xml中,

<Button
    android:id="@+id/btnFirstRecordxml"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/pszFirstRecordButton" 
    android:onClick="Tab3Fragment.firstRecord"/>

处理按钮点击将不胜感激。提前谢谢...

2014 年 11 月 23 日更新。 这是一个适用于 showPopup() 中多个按钮代码的解决方案。我将在弹出窗口中有几个按钮....

private void showPopup(){
    LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
    View popupView = layoutInflater.inflate(R.layout.popup_layout, null);
    Button btnDismiss=(Button)popupView.findViewById(R.id.btnDismissxml);
    Button btnSaveRecord=(Button)popupView.findViewById(R.id.btnSaveRecordxml);
    final PopupWindow popupWindow=new PopupWindow(popupView,480,500,true);
    popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 40);                                   
//first button
    btnSaveRecord.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            saveRecord();
        }
    });
 //second button
    btnDismiss.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
        }
    });
}

有什么方法可以插入 case-switch 结构以允许 showPopup() 中的其他按钮代码?这将避免为每个按钮创建单独的 onClickListener-onClicks,如上所示。

【问题讨论】:

    标签: android android-fragments popup onclicklistener buttonclick


    【解决方案1】:

    像这样修改xml,在所有按钮中使用android:onClick="ClickEvent"这个格式

    <Button
    android:id="@+id/btnFirstRecordxml"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/pszFirstRecordButton" 
    android:onClick="ClickEvent"/>
    <Button
    android:id="@+id/btnPreviousRecordxml"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/pszPreviousRecordButton" 
    android:onClick="ClickEvent"/>
    

    像这样使用点击功能,

    public void clickEvent(View v)
    {
        switch (v.getId()) {
    case R.id.btnFirstRecordxml:
        //firstRecord(v);
        break;
    case R.id.btnPreviousRecordxml:
        //previousRecord(v);
        break;
    case R.id.btnNextRecordxml:
        //nextRecord(v);
        break;
    case R.id.btnLastRecordxml:
        //lastRecord(v);
        break;
    case R.id.btnDismissxml:
        //closePopup(v);
        break;  
    }
    }  
    

    或者试试这个,

    btn.setOnClickListener(btnaction); 为所有按钮设置这样的监听器,

     OnClickListener btnaction = new OnClickListener() {
       @Override
       public void onClick(View v) {
         //use the switch conditons...
       }
     };
    btn1.setOnClickListener(btnaction);
    btn2.setOnClickListener(btnaction);
    

    【讨论】:

    • 有没有办法格式化 clickEvent(View v) 以允许代码驻留在 showPopup 方法中?谢谢!
    • 以前 clickEvent 在片段 Tab3Fragment 中的 showPopup 之外可以正常工作,但在 showPopup 内部不能正常工作。你问的是这个吗?
    • 我添加了一些最新的代码,试试那个代码告诉我,它有效吗?
    • 谢谢普拉卡什。我已经用您建议的代码更新了我的问题 - 但是我认为我没有实现您的意图。再次感谢。
    • 在您的 xml 中的所有按钮中删除此 android:onClick="Tab3Fragment.firstRecord" 并尝试一下。
    【解决方案2】:
    public void showPopup(View anchorView) {    
        Button btnDismiss, btnFirstRecord, btnPreviousRecord, btnNextRecord, btnLastRecord;
        LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemServi (Context.LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.popup_layout, null);
        final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);    
        TextView tv = (TextView)popupView.findViewById(R.id.tv);
        tv.setText("Blah, blah, blah");
    
        btnDismiss = (Button)popupView.findViewById(R.id.btnDismissxml);
        btnFirstRecord = (Button)popupView.findViewById(R.id.btnFirstRecordxml);
        btnPreviousRecord = (Button)popupView.findViewById(R.id.btnPreviousRecordxml);
        btnNextRecord = (Button)popupView.findViewById(R.id.btnNextRecordxml);
        btnLastRecord = (Button)popupView.findViewById(R.id.btnLastRecordxml);
    
    btnFirstRecord.setOnClickListener(new Listener());
    btnPreviousRecord.setOnClickListener(new Listener());
    btnNextRecord .setOnClickListener(new Listener());
    btnLastRecord .setOnClickListener(new Listener()); 
    
        popupWindow.setFocusable(true);
        int location[] = new int[2];
        anchorView.getLocationOnScreen(location);
        popupWindow.showAtLocation(anchorView, Gravity.CENTER, 0, 0);
        }   
    }
    
    
    Class Listener implements OnClickListener
    {
    @Override
            public void onClick(View v) {
     switch (v.getId()) {
        case R.id.btnFirstRecordxml:
            //firstRecord(v);
            break;
        case R.id.btnPreviousRecordxml:
            //previousRecord(v);
            break;
        case R.id.btnNextRecordxml:
            //nextRecord(v);
            break;
        case R.id.btnLastRecordxml:
            //lastRecord(v);
            break;
        case R.id.btnDismissxml:
            //closePopup(v);
            break;  
        }
    
    }
    
    }
    

    【讨论】:

    • 谢谢罗希特。我已经得到了与您发布的内容非常相似的内容。我正在寻找一种将“public void onClick(View v)”移动到 showPopup 内部的方法......或者可能在 xml 文件中调用了 onClick。你认为这可能吗?问候。
    • 只有在将 clicklistener 设置为单个按钮时才可行,否则在 showpop 方法中是不可能的
    • 我认为关于 showpop() 中的 clicklisteners,我得出了相同的结论。从 XML 文件中发送的 onClick 怎么样?尝试此操作时,我无法让 popup_layout.xml 文件发送 onClick 按钮事件。感谢您对此的回复。
    • 当您在 xml 中使用 onclick 时,它仅在您在 onCreate 函数中使用 setContentView 并且 popup 用作另一个布局时才有效,因此无法在活动中使用 popup 布局的 onclick 方法跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多