在函数中传递参数,然后调用onClick函数
Passing parameters in function and then calling function onClick
我正试图获得一个按钮来调用一个函数。唯一的问题是,该函数从另一个函数调用了一个参数,我得到了一个错误: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);
}
});
祝好运
相关文章:
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 根据函数参数将CSS值存储在var中,然后对其进行操作
- 选择值,然后只调用函数
- 如何用javascript调用函数,然后在滚动事件中调用该函数的特定实例
- 使用变量的名称,然后为该变量定义函数
- 加载文件,然后调用回调函数
- 等待函数完成,然后将返回值分配给变量JS
- 使用函数for循环遍历对象以更改值,然后返回结果
- 如何每 10 秒调用一次 JS 函数,然后以角度激活一个函数
- 如何让 setTimeout 函数运行,然后停止使用循环
- 蓝鸟与猫鼬,类型错误:.create(..).然后(..).Nodeify不是一个函数
- Javascript:编写一个函数,接收一个数组,然后返回一个只有唯一数字的数组,只删除数组
- 如何运行一个调用某个函数的函数首先完成它,然后再调用下一个函数.正在创建回调函数
- Javascript Promise().然后防止在执行第一个调用之前重新调用该函数
- 需要一个文件,然后使用一个函数,而不显式地将所需文件用作命名空间
- 如何计算一个函数在javascript中运行的次数?然后在页面上显示
- 如何从按钮后面的代码中调用javascript函数,点击并存储其输出,然后继续操作
- 将字符串传递给javascript函数然后显示onClick
- 要查找的函数然后执行任务