我執行在片段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,如上所示。
有沒有格式化clickEvent(View v)以允許代碼駐留在showPopup方法中的方法?謝謝! – portsample 2014-11-22 04:57:26
clickevent函數是否有效? – prakash 2014-11-22 04:59:07
以前,clickEvent在片段Tab3Fragment中的showPopup之外正常工作,但不在showPopup中。你在問什麼? – portsample 2014-11-22 05:28:41