【发布时间】:2018-11-14 14:39:17
【问题描述】:
为什么我不能在fab.setOnClickListener(View 的onCreate 方法中创建弹出对话框?
是因为语法吗?我缺少表达吗? 我的代码是否已经过时了,因为我的导师是一门古老的 udemy 课程?
public class MainActivity extends AppCompatActivity {
private AlertDialog.Builder dialogBuilder;
private AlertDialog dialog;
private EditText groceryItem;
private EditText getGroceryItem;
private EditText quantity;
private Button saveButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
// === error appears on the next line ===
fab.setOnClickListener(View,
createPopupDialog());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void createPopupDialog() {
dialogBuilder = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.popup, null);
groceryItem = view.findViewById(R.id.groceryItem);
quantity = view.findViewById(groceryQty);
saveButton = view.findViewById(R.id.saveButton);
dialogBuilder.setView(view);
dialog = dialogBuilder.create();
dialog.show();
}
}
【问题讨论】:
-
什么是
View作为参数传递?setOnClickListener在这里期待什么?
标签: java android xml android-layout