在函数中传递参数然后调用函数onClick

时间:2016-04-09 21:36:25

标签: javascript android function

我试图按下一个按钮来调用一个函数。唯一的问题是,该函数从另一个函数调用一个参数,并且我得到一个错误:AddRecipe中的SetRecipe(int)不能应用于()。我不知道我的意思是通过/调用正确的术语。

按钮代码为:

    Button saveRecipe = (Button) findViewById(R.id.btnAddRecipe);
    saveRecipe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SetRecipe();
        }
    });

功能是:

public int getDifficulty (View v)
{
    int Difficulty = -1;
    boolean checked = ((RadioButton) v).isChecked();
    switch(v.getId()) {
        case R.id.btnDiff1:
            if (checked)
                Difficulty = 1;
            break;
        case R.id.btnDiff2:
            if (checked)
                Difficulty = 2;
            break;
        case R.id.btnDiff3:
            if (checked)
                Difficulty = 3;
            break;
        case R.id.btnDiff4:
            if (checked)
                Difficulty = 4;
            break;
        case R.id.btnDiff5:
            if (checked)
                Difficulty = 5;
            break;
    }

    return Difficulty;
}

public void SetRecipe (int Difficulty)
{
    TextView RecipeNameView = (TextView) findViewById(R.id.editRecipeName);
    TextView CookTimeView = (TextView) findViewById(R.id.editCookTime);
    Spinner MealTypeView = (Spinner) findViewById(R.id.editMeal);
    TextView HowToView = (TextView) findViewById(R.id.editHowTo);

    String RecipeName = RecipeNameView.getText().toString();
    int CookTime = Integer.parseInt(CookTimeView.getText().toString());
    String MealType = MealTypeView.getSelectedItem().toString();
    String HowTo = HowToView.getText().toString();


    DatabaseHandler dbh = new DatabaseHandler(this);
    dbh.SetRecipe(RecipeName, CookTime, Difficulty, MealType, HowTo);

}

我要做的就是从getDifficulty函数中获取int Difficulty并将其传递给SetRecipe函数。当用户点击按钮时,将调用SetRecipe函数。

如果这是一个愚蠢的错误,那么我道歉,我对编码非常陌生,所以我不知道自己在做什么。任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:0)

而不是说难度= 1,2,3,4你能直接称它吗?

SetRecipe(1);

另外......当我实现View.OnClickListener ...

public class yourclass extends AppCompatActivity implements View.OnClickListener {
Button setrecipe1;
Button setrecipe2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourlayout);

    setrecipe1= (Button) findViewById(R.id.setrecipe1);
    setrecipe2= (Button) findViewById(R.id.setrecipe2);
    setrecipe1.setOnClickListener(this);
    setrecipe2.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.setrecipe1:
            SetRecipe(1);
            break;
        case R.id.setrecipe2:
            SetRecipe(2);
            break;
        default:
            break;
}
}
}

答案 1 :(得分:0)

尝试将android:onClick="getDifficulty"属性添加到btnAddRecipe标记并删除onClickListener ...
这应该工作

答案 2 :(得分:0)

OnCreate方法声明之前

int Difficulty;

getDifficulty方法中删除int

int Difficulty = -1;  ==> Difficulty = -1;

然后在你的点击按钮内:

Button saveRecipe = (Button) findViewById(R.id.btnAddRecipe);
saveRecipe.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        SetRecipe(Difficulty);
    }
});
祝你好运

相关问题