我目前遇到了一個問題,我有一個叫做'PopupWindow'的超類,它初始化了一個AlertDialog.Builder並將其顯示給用戶。不過,我有兩個名爲'CallInitializePopup'和'CallReinitializePopup'的子類。我想將輸入監聽器(onClick)「外包」到這些子類中,並將代碼單獨分配給在那裏調用的子類。Android - OnClick在子類中的使用,而不是超級
片段PopupWindow的:
private void setInputListener()
{
Log.d("setInputListener", "called");
alert.setPositiveButton("Set Alert", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Store the values in current variables
stockSymbol = stockSymbolEditText.getText().toString();
stockPrice = stockPriceEditText.getText().toString();
//String selectedInterval = updateIntervalSpinner.getSelectedItem().toString();
buyOrSell = buySellSpinner.getSelectedItem().toString();
Log.d("Alert dialog submitted", "stockSymbol: " + stockSymbol + " - " + "stockPrice: " + stockPrice + " - " + "buyOrSell: " + buyOrSell);
//Only call 'AssignStockCall()' once stockSymbol, stochPrice, and buyOrSell are initialized in the onClick method
//Create a new StockCall with the new info the user included
AssignNewStockCall();
}
});
//With 'setNegativeButton' we don't want to do anything, the user doesn't want to add a new stock call
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Canceled
}
});
}
第一種方法不顯示任何按鈕,如果我是以此來猜測這就是:在一個子類
alert = new AlertDialog.Builder(mainActivity);
//'setTitle' simply sets the title of the popup
//'setMessage' sets the description, usually a short instruction on what the user should enter
alert.setTitle(POPUP_LOGIN_TITLE);
alert.setMessage(POPUP_LOGIN_TEXT);
//Initialize EditTexts that will populate our popup and set the hints for each
stockSymbolEditText = new EditText(mainActivity);
stockSymbolEditText.setHint(STOCK_SYMBOL_HINT);
stockPriceEditText = new EditText(mainActivity);
stockPriceEditText.setHint(STOCK_PRICE_HINT);
//These TextViews are only there to give the user guidance on what to include regarding the Spinners(since the Spinners doesn't include hints)
buyOrSellTextView = new TextView(mainActivity);
buyOrSellTextView.setText(" Alert - buy or sell");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 35);
LinearLayout layout = new LinearLayout(mainActivity);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(stockSymbolEditText, layoutParams);
layout.addView(stockPriceEditText, layoutParams);
//layout.addView(updateIntervalTextView);
//layout.addView(updateIntervalSpinner);
layout.addView(buyOrSellTextView);
layout.addView(buySellSpinner);
alert.setView(layout);
//Finally we show the popup
alert.show();
我的第一個OnClickListener方法因爲我們在另一個類之後初始化inputListener,而不是初始化AlertDialog.Builder。
第二種方法:
//These onClick classes are used by the PopupWindow class, they are assigned to the specific button by supplying a new instance of the classes
final class CancelOnClickListener implements
DialogInterface.OnClickListener
{
public void onClick(DialogInterface dialog, int which)
{
Log.d("onClick", "Cancel");
}
}
final class SetAlertOnClickListener implements
DialogInterface.OnClickListener
{
public void onClick(DialogInterface dialog, int which)
{
Log.d("onClick", "Set Alert");
}
}
這種做法不與超類的工作,因爲超類需要知道它是否是一個「CallInitializePopup」或「CallReinitializePopup」並沒有超級和子類之間的這種通信。
任何幫助,高度讚賞!
感謝您的建議!我對OOP概念很陌生,我一定會看看Derek Banas的視頻。 –