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

Passing parameters in function and then calling function onClick

本文关键字:函数 然后 调用 onClick 参数      更新时间:2024-04-23

我正试图获得一个按钮来调用一个函数。唯一的问题是,该函数从另一个函数调用了一个参数,我得到了一个错误: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);
}

我想做的就是从getDistriction函数中获取int难度,并将其传递给SetRecipe函数。当用户点击按钮时,SetRecipe函数将被调用。

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

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

不要说难度=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;
}
}
}

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);
    }
});

祝好运