我有一个带有EditText输入字段的对话窗口。我想多次使用输入值,但希望尽可能减少代码。目前我必须为每次按钮点击设置我的alertdialogBuilder新功能,但这不是很灵活。
我将该代码用于我的alertDialogBuilder
[0] ==> API Server now listening on PORT 5000!
[1] Starting the development server...
[1]
[1] Compiled successfully!
[1]
[1] You can now view chat app in the browser.
[1]
[1] Local: http://localhost:3000/
[1] On Your Network: http://192.168.1.118:3000/
[1]
[1] Note that the development build is not optimized.
[1] To create a production build, use yarn build.
我如何设置我的类或函数,以便我可以创建一个函数,我可以用点添加更多函数
:public static void makeEditTextInputDialog(String caption, final Context mContext){
LayoutInflater inflater = LayoutInflater.from(mContext);
View inputDialogView = inflater.inflate(R.layout.dialog_input_edittext, null);
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setView(inputDialogView);
final EditText etInput = (EditText) inputDialogView.findViewById(R.id.et_inputdialog);
final TextView tvCaption = (TextView) inputDialogView.findViewById(R.id.tv_inputdialog_caption);
tvCaption.setText(caption);
builder .setCancelable(true)
.setPositiveButton(R.string.apply, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
答案 0 :(得分:1)
您应该能够通过使每个方法返回this
作为您正在创建的Builder类的实例来实现此目的。这样您就可以将呼叫与'。'
这是一个简单的例子:
class MessageBuilder {
String address = "unknown";
String state = "unknown";
String country = "unknown";
MessageBuilder() {}
String show() {
return address + ", " + state + ", " + country;
}
MessageBuilder setAddress(String address) {
this.address = address;
return this;
}
MessageBuilder setState(String state) {
this.state = state;
return this;
}
MessageBuilder setCountry(String country) {
this.country = country;
return this;
}
}
通过将set方法链接在一起来使用它:
MessageBuilder builder = new MessageBuilder();
String msg = builder.setState("CA").setAddress("123 Main St.").show();
Log.d("MessageBuilder", msg);
...在日志中产生这个:
09-24 14:25:16.254 {...} D/MessageBuilder: 123 Main St., CA, unknown