Java 单击按钮后在对话框上启动活动
我是Android Studio中的一个noob,现在我一直在创建我的应用程序。我在我的主要活动中创建了一个浮动操作按钮,然后单击它,我想显示一个对话框,询问用户该做什么。在这种情况下,我有两个选项可供选择,单击每个选项,我想打开两个不同的活动,名为AddProductNoEan和AddProductEan。我尝试使用下面的代码,但在onCreate结束时关闭最后一个大括号后出现错误。我已经在其他应用程序中使用了相同的过程,并且效果很好,因此在以不同方式尝试多次但未成功后,我完全卡住了!有没有人有一些提示来解决这个问题,或者有更好的方法来实现我的目标?提前谢谢<代码>Java 单击按钮后在对话框上启动活动,java,android,button,android-studio,dialog,Java,Android,Button,Android Studio,Dialog,我是Android Studio中的一个noob,现在我一直在创建我的应用程序。我在我的主要活动中创建了一个浮动操作按钮,然后单击它,我想显示一个对话框,询问用户该做什么。在这种情况下,我有两个选项可供选择,单击每个选项,我想打开两个不同的活动,名为AddProductNoEan和AddProductEan。我尝试使用下面的代码,但在onCreate结束时关闭最后一个大括号后出现错误。我已经在其他应用程序中使用了相同的过程,并且效果很好,因此在以不同方式尝试多次但未成功后,我完全卡住了!有没有人
公共类清单扩展了ActionBarActivity
实现NavigationDrawerFragment.NavigationDrawerCallbacks{
浮动操作按钮添加;
AlertDialog.Builder构建;
/**
*片段管理导航抽屉的行为、交互和表示。
*/
私人导航抽屉碎片mnavigation抽屉碎片;
/**
*用于存储最后一个屏幕标题。用于{@link#restoreActionBar()}。
*/
私有字符序列mTitle;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory);
mNavigationDrawerFragment=(导航DrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation\u抽屉);
mTitle=getTitle();
//把抽屉摆好。
mNavigationDrawerFragment.setUp(
R.id.navigation\u抽屉,
(抽屉布局)findViewById(R.id.抽屉布局);
ImageView图标=新建ImageView(此);//创建图标
icon.setImageResource(R.drawable.ic\u action\u new);
add=new FloatingActionButton.Builder(此)
.setContentView(图标)
.设置位置(4)
.build();
add.setOnClickListener(新的AdapterView.OnItemLongClickListener(){
@凌驾
公共布尔值长单击(AdapterView父项、视图、整型位置、长id){
build=newalertdialog.Builder(Inventory.this);
build.setTitle(R.string.delete);
build.setMessage(R.string.question\u delete);
build.setPositiveButton(R.string.yes,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
Intent noean=新的Intent(Inventory.this,AddProductNoEan.class);
星触觉;
dialog.cancel();
}
});
build.setNegativeButton(R.string.no,new DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
意向eans=新意向(Inventory.this,AddProductEan.class);
星触觉(eans);
dialog.cancel();
}
});
AlertDialog alert=build.create();
alert.show();
返回false;
}
}};
@凌驾
已选择NavigationDrawerItems上的公共无效(整数位置){
//通过替换片段更新主要内容
FragmentManager FragmentManager=getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container,PlaceholderFragment.newInstance(位置+1))
.commit();
}
附加的公共无效部分(整数){
开关(编号){
案例1:
mTitle=getString(R.string.title_section1);
Intent adne=新Intent(Inventory.this,AddProductNoEan.class);
星触觉(adne);
打破
案例2:
mTitle=getString(R.string.title_第2节);
意向列表A1=新意向(Inventory.this,listaprotottinoean.class);
星触觉(列表A1);
打破
案例3:
mTitle=getString(R.string.title_第3节);
Intent ade=新Intent(Inventory.this,AddProductEan.class);
星触觉(ade);
打破
案例4:
mTitle=getString(R.string.title\u activity\u lista\u prodotti\u ean);
意向列表A2=新意向(Inventory.this,listaprotottiean.class);
星触觉(列表A2);
打破
案例5:
mTitle=getString(R.string.title\u activity\u all\u products);
意向ap=新意向(Inventory.this,All_Products.class);
星触觉;
打破
案例6:
mTitle=getString(R.string.title\u activity\u gallery);
意向ga=新意向(Inventory.this,Gallery.class);
星触觉;
打破
}
}
public void restoreActionBar(){
ActionBar ActionBar=getSupportActionBar();
actionBar.setNavigationMode(actionBar.NAVIGATION_模式_标准);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
如果(!mNavigationDrawerFragment.isDrawerOpen()){
//仅在操作栏中显示与此屏幕相关的项目
//如果抽屉没有显示。否则,让抽屉显示
//决定在动作栏中显示什么。
getMenuInflater().充气(R.menu.inventory,menu);
restoreActionBar();
返回true;
}
返回super.onCreateOptions菜单(菜单);
}
@凌驾
public class Inventory extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
FloatingActionButton add;
AlertDialog.Builder build;
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
ImageView icon = new ImageView(this); // Create an icon
icon.setImageResource(R.drawable.ic_action_new);
add = new FloatingActionButton.Builder(this)
.setContentView(icon)
.setPosition(4)
.build();
add.setOnClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
build = new AlertDialog.Builder(Inventory.this);
build.setTitle(R.string.delete);
build.setMessage(R.string.question_delete);
build.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent noean = new Intent(Inventory.this, AddProductNoEan.class);
startActivity(noean);
dialog.cancel();
}
});
build.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent eans = new Intent(Inventory.this, AddProductEan.class);
startActivity(eans);
dialog.cancel();
}
});
AlertDialog alert = build.create();
alert.show();
return false;
}
}};
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
Intent adne = new Intent(Inventory.this, AddProductNoEan.class);
startActivity(adne);
break;
case 2:
mTitle = getString(R.string.title_section2);
Intent lista1 = new Intent(Inventory.this, ListaProdottiNoEan.class);
startActivity(lista1);
break;
case 3:
mTitle = getString(R.string.title_section3);
Intent ade = new Intent(Inventory.this, AddProductEan.class);
startActivity(ade);
break;
case 4:
mTitle = getString(R.string.title_activity_lista_prodotti_ean);
Intent lista2 = new Intent(Inventory.this, ListaProdottiEan.class);
startActivity(lista2);
break;
case 5:
mTitle = getString(R.string.title_activity_all__products);
Intent ap = new Intent(Inventory.this, All_Products.class);
startActivity(ap);
break;
case 6:
mTitle = getString(R.string.title_activity_gallery);
Intent ga = new Intent(Inventory.this, Gallery.class);
startActivity(ga);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.inventory, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_inventory, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((Inventory) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}