0

我執行在片段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方法的情況下,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"/> 

處理按鈕點擊將高度讚賞。在此先感謝...

更新,11月23日2014年 這裏是不工作的多個按鈕的代碼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,如上所示。

回答

0

修改這樣的XML,使用android:onClick="ClickEvent"這個formet中的所有按鈕

<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"/> 

使用點擊funcationality這樣,

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); 
+0

有沒有格式化clickEvent(View v)以允許代碼駐留在showPopup方法中的方法?謝謝! – portsample 2014-11-22 04:57:26

+0

clickevent函數是否有效? – prakash 2014-11-22 04:59:07

+0

以前,clickEvent在片段Tab3Fragment中的showPopup之外正常工作,但不在showPopup中。你在問什麼? – portsample 2014-11-22 05:28:41

0
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; 
    } 

} 

} 
+0

謝謝Rohit。我已經有了一些非常類似於你的東西已發佈。我正在尋找一種將「public void onClick(View v)」移動到showPopup內部的方法...或者可能在xml文件中調用onClick。你認爲這可能嗎?問候。 – portsample 2014-11-23 05:32:33

+1

只有當您將clicklistener設置爲單獨的按鈕時纔有可能,其他明智的方法在showpop方法中不可行 – 2014-11-24 04:15:17

+0

我認爲我已經對showpop()中的clicklisteners得出了相同的結論。那麼從XML文件發送的onClick怎麼樣?嘗試此操作時,我無法讓popup_layout.xml文件發送onClick按鈕事件。感謝您對此的迴應。 – portsample 2014-11-24 04:27:23