【问题标题】:accessing variable in a method to another method in same class将方法中的变量访问到同一类中的另一个方法
【发布时间】:2015-09-16 19:11:48
【问题描述】:

您好,我已经习惯了使用材料设计库的输入对话框来获取值。

在保存按钮里面我放了我的输入对话框

saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



                new MaterialDialog.Builder(getActivity())
                        .title("Please enter your playlist name")
                        .inputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_DATETIME_VARIATION_NORMAL)
                        .input("", "", new MaterialDialog.InputCallback() {
                            @Override
                            public void onInput(MaterialDialog dialog, CharSequence input) {

                                new BackgroundTask().execute();
                            }
                        }).show();



            }
        });

我想在这里获取“输入”变量

 public void onInput(MaterialDialog dialog, CharSequence input) {

在其他方法中使用。我怎样才能访问这个变量?

【问题讨论】:

  • 你不能将“输入”作为参数传递给其他方法吗?

标签: android variables methods


【解决方案1】:

如果我理解你的问题,你可以得到这样的输入值:

private inputVal;

.... 
@Override
public void onInput(MaterialDialog dialog, CharSequence input) {
   //save the input variable to a global variable to be able to use it later
   inputVal = input.toString();
   new BackgroundTask().execute();
}

然后您可以轻松地在其他方法中使用输入变量

【讨论】:

    【解决方案2】:

    您可以将变量直接传递给方法或将其保存到类变量中然后使用它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 2015-01-21
      相关资源
      最近更新 更多