我目前遇到了一个问题,我有一个名为' PopupWindow'它初始化了一个AlertDialog.Builder并将其显示给用户。但是我有两个子类叫做CallInitializePopup'和' CallReinitializePopup'。我想"外包"这些子类的输入监听器(onClick),并且具有代码所在的子类的单独代码。
PopupWindow片段:
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方法:
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
}
});
}
第一种方法没有显示任何按钮,如果我要猜测是因为我们在另一个类之后初始化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”' CallInitializePopup'或者“CallReinitializePopup”#39;超级和次级之间没有这种沟通。
非常感谢任何帮助!
答案 0 :(得分:1)
如果你不改变你的编码风格,你将来会遇到很多这样的问题。您应该首先学习面向对象编程的SOLID原则。 https://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29然后研究设计模式。 Derek Banas youtube频道得到了帮助,因为他非常清楚地解释了设计模式。 在您的情况下,您违反了开放封闭原则(来自SOLID原则)。超类不应该依赖于子类。 在你的情况下,我根本不会使用继承。如果您需要对同一操作执行不同的操作,请使用策略或状态设计模式。
class PopUpWindow implements DialogInterface.OnClickListener
{
/**
*
*
* other code
*
**/
/** You don't need two different listers. Use same listener since
you can determine which button has been pressed from *int which* **/
alert.setPositiveButton(this);
alert.setNegativeButton(this);
interface Strategy
{
public void doWork();
}
@Override
public void onClick(DialogInterface dialog, int which)
{
switch(which)
{
case dialog.BUTTON_POSITIVE :
getPositiveButtonStrategy().doWork(); break;
case dialog.BUTTON_NEGATIVE :
getNegativeButtonStrategy().doWork(); break;
}
}
private Strategy getPositiveButtonStrategy()
{
if (/** conditions to implementation NO1**/)
return new Strategy
{
@Override
public void doWork()
{
/** your implementation NO1 **/
}
}
else return new Strategy
{
@Override
public void doWork()
{
/** your implementation NO2 **/
}
};
/** you can implement as much Strategies as you need **/
}
}
private Strategy getNegativeButtonStrategy()
{
if (/** conditions to implementation NO1**/)
return new Strategy
{
@Override
public void doWork()
{
/** your implementation NO1 **/
}
}
else return new Strategy
{
@Override
public void doWork()
{
/** your implementation NO2 **/
}
};
/** everything in your class is available for "Strategies" **/
}
}
}
在不了解SOLID和设计模式的情况下,编写可理解,可维护,无错误的代码几乎是不可能的