【问题标题】:Adding confirmation action dialog before changing selected Action Tab?在更改选定的操作选项卡之前添加确认操作对话框?
【发布时间】:2014-06-12 05:18:09
【问题描述】:

我试图让用户在选择新选项卡之前确认(通过 AlertDialog)他们想要切换选项卡。我正在使用 ActionBarSherlock 库(因为我想在 Android 2.3 及更高版本中编译)在我的 Android 布局中创建操作选项卡,并且可以看到何时选择了新选项卡,以及何时取消选择。目前,我用它来知道何时要求用户确认开关,如下所示:

    ActionBar ab;
    String newTab;

    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    ab = getSupportActionBar();
    newTab=tab.getText().toString();

    new AlertDialog.Builder(this)
    .setTitle("Continue?")
    .setMessage("Are you sure you want to continue?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // User confirms that they want to switch tabs
            if (newTab.equals("tab 1")) {
                setContentView(R.layout.tab1);
            } else if (newTab.equals("tab 2")) {
                setContentView(R.layout.tab2);
            } 
        }
     })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // DONT switch tabs
            ab.selectTab(prevTab);
        }
     })
    .setIcon(android.R.drawable.ic_dialog_alert)
    .show();

    // update the previous tab
    prevTab = ab.getSelectedTab();
    }

但是,如果用户想选择“否”(不要切换标签),库似乎已经选择了它?是否可以控制在库选择此新选项卡之前发生的情况,然后在实际切换之前要求用户确认?

谢谢!

【问题讨论】:

    标签: java android tabs actionbarsherlock android-alertdialog


    【解决方案1】:

    使用setContentView() 命令不是你认为最好的解决方案。我建议使用 TabHost。

    编辑:

    MainActivity.java:

    package com.example.teszt;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.os.Build;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.TabHost;
    
    
    public class MainActivity extends Activity {
    
        private TabHost tabs;
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_main);
    
            tabs = (TabHost) findViewById(R.id.tabhost);
            tabs.setup();
    
            TabHost.TabSpec Favorit = tabs.newTabSpec("Favorit");
            Favorit.setContent(R.id.favorite);
            Favorit.setIndicator((Build.VERSION.SDK_INT > 10) ? "Favorit" : "");
            tabs.addTab(Favorit);
    
            TabHost.TabSpec All_line = tabs.newTabSpec("All");
            All_line.setContent(R.id.all);
            All_line.setIndicator((Build.VERSION.SDK_INT > 10) ? "All" : "");
            tabs.addTab(All_line);
    
            for(int i = 0; i < tabs.getTabWidget().getChildCount(); ++i){
                tabs.getTabWidget().getChildAt(i).setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        ShowDialog();
                    }
                });
            }
        }
    
        private void ShowDialog(){
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                alertDialog.setTitle("Continue?");
                alertDialog.setMessage("Are you sure you want to continue?");
                alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if(tabs.getCurrentTab() == 0){
                            tabs.setCurrentTab(1);
                        }else{
                            tabs.setCurrentTab(0);
                        }
                    }
                });
                alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // Do nothing
                    }
                });
                alertDialog.setCancelable(false);
                alertDialog.show();
        }
    }
    

    framgment_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <TabHost
            android:id="@+id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
    
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
    
            </TabWidget>
    
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:paddingTop="48dp" >
    
                <TableLayout
                    android:id="@+id/favorite"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >
    
                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="tab1" />
    
                </TableLayout>
    
                <TableLayout
                    android:id="@+id/all"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >
    
                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="tab2" />
    
                </TableLayout>
    
            </FrameLayout>
    
        </TabHost>
    
    </LinearLayout>
    

    希望能帮到你!

    【讨论】:

    • 谢谢!但是,可以在 Android 2.3 中实现 TabHosts(这是我要编译的内容)吗?相反,是否可以使用 ActionBarSherlock 来做同样的事情?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多