java - 在更改所选操作选项卡之前添加确认操作对话框?

标签 java android tabs actionbarsherlock android-alertdialog

我试图要求用户在选择新选项卡之前确认(通过 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();
    }

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

谢谢!

最佳答案

使用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>

希望能帮到你!

关于java - 在更改所选操作选项卡之前添加确认操作对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24176925/

相关文章:

java - 通用接口(interface)和通配符

android - 无法检测文件 : java. lang.NoClassDefFoundError android/content/ComponentCallbacks

android - 如何通过点击推送消息通知来判断应用程序已启动

jquery - 如何获取 JQuery UI 选项卡插件中默认加载的选项卡文本?

bash - 如何在 bash 变量中转义换行符和制表符?

java - 无法在 Android Studio 中打开新的 Activity

java - 无法构造 java.util.LinkedHashMap : no String-argument constructor/factory 的实例

java - JUnit 混淆 : use 'extends TestCase' or '@Test' ?

android - 为弃用/newapi 自定义 lint 警告

android - 在使用带有选项卡布局的自定义 View 时加载 Activity 时突出显示 View 寻呼机中的第一项?