如何将参数传递给函数?
How to pass in a parameter into a function?
我正在尝试在我的 addCard 函数中添加 addcard 名称参数 R.id.btn_billHistory:
如何在其中传递名称参数?我不确定如何解决它。这行给我一个错误。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.btn_billHistory:
//Toast.makeText(getApplicationContext(),"Bill History",Toast.LENGTH_LONG).show();
Intent i = new Intent(MainActivity.this,BillActivity.class);
startActivity(i);
finish();
**addCard();**
return true;
case R.id.btn_about:
openCloseDialog();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void **addCard(String name)** {
final View view = getLayoutInflater().inflate(R.layout.activity_bill, null);
TextView nameView = view.findViewById(R.id.edtName);
Button delete = view.findViewById(R.id.btn_delete);
nameView.setText(name);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layout.removeView(view);
}
});
layout.addView(view);
}
您需要在开始前传递addCard()
函数activity
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.btn_billHistory:
//Toast.makeText(getApplicationContext(),"Bill History",Toast.LENGTH_LONG).show();
addCard("Check Bill History");
Intent i = new Intent(MainActivity.this,BillActivity.class);
startActivity(i);
finish();
return true;
case R.id.btn_about:
openCloseDialog();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void **addCard(String name)** {
final View view = getLayoutInflater().inflate(R.layout.activity_bill, null);
TextView nameView = view.findViewById(R.id.edtName);
Button delete = view.findViewById(R.id.btn_delete);
nameView.setText(name);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layout.removeView(view);
}
});
layout.addView(view);
}
您需要将字符串传递给 onOptionsItemSelected
中的 addCard
调用。
只是浏览一下您的代码片段,我猜它是“Bill History”,但您传递的字符串由您决定。
例如:
case R.id.btn_billHistory:
Intent i = new Intent(MainActivity.this,BillActivity.class);
startActivity(i);
finish();
addCard("Bill History");
return true;
但是,只注意到您的 finish
调用,如果您只是要开始另一个 activity,则更新当前 activity 中的任何视图都没有意义。所以你可能想重新考虑一下。
我正在尝试在我的 addCard 函数中添加 addcard 名称参数 R.id.btn_billHistory: 如何在其中传递名称参数?我不确定如何解决它。这行给我一个错误。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.btn_billHistory:
//Toast.makeText(getApplicationContext(),"Bill History",Toast.LENGTH_LONG).show();
Intent i = new Intent(MainActivity.this,BillActivity.class);
startActivity(i);
finish();
**addCard();**
return true;
case R.id.btn_about:
openCloseDialog();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void **addCard(String name)** {
final View view = getLayoutInflater().inflate(R.layout.activity_bill, null);
TextView nameView = view.findViewById(R.id.edtName);
Button delete = view.findViewById(R.id.btn_delete);
nameView.setText(name);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layout.removeView(view);
}
});
layout.addView(view);
}
您需要在开始前传递addCard()
函数activity
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.btn_billHistory:
//Toast.makeText(getApplicationContext(),"Bill History",Toast.LENGTH_LONG).show();
addCard("Check Bill History");
Intent i = new Intent(MainActivity.this,BillActivity.class);
startActivity(i);
finish();
return true;
case R.id.btn_about:
openCloseDialog();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void **addCard(String name)** {
final View view = getLayoutInflater().inflate(R.layout.activity_bill, null);
TextView nameView = view.findViewById(R.id.edtName);
Button delete = view.findViewById(R.id.btn_delete);
nameView.setText(name);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layout.removeView(view);
}
});
layout.addView(view);
}
您需要将字符串传递给 onOptionsItemSelected
中的 addCard
调用。
只是浏览一下您的代码片段,我猜它是“Bill History”,但您传递的字符串由您决定。
例如:
case R.id.btn_billHistory:
Intent i = new Intent(MainActivity.this,BillActivity.class);
startActivity(i);
finish();
addCard("Bill History");
return true;
但是,只注意到您的 finish
调用,如果您只是要开始另一个 activity,则更新当前 activity 中的任何视图都没有意义。所以你可能想重新考虑一下。