Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否在更改所选操作选项卡之前添加确认操作对话框?_Java_Android_Tabs_Actionbarsherlock_Android Alertdialog - Fatal编程技术网

Java 是否在更改所选操作选项卡之前添加确认操作对话框?

Java 是否在更改所选操作选项卡之前添加确认操作对话框?,java,android,tabs,actionbarsherlock,android-alertdialog,Java,Android,Tabs,Actionbarsherlock,Android Alertdialog,在选择新选项卡之前,我试图要求用户确认(通过AlertDialog)他们想要切换选项卡。我正在使用ActionBarSherlock库(因为我想在Android 2.3及更高版本中编译)在我的Android布局中创建操作选项卡,并且可以查看何时选择和取消选择新选项卡。目前,我使用它来知道何时要求用户确认切换,如下所示: ActionBar ab; String newTab; public void onTabUnselected(ActionBar.Tab tab,

在选择新选项卡之前,我试图要求用户确认(通过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();
    }
}
package com.example.teszt;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.DialogInterface;
导入android.os.Build;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.TabHost;
公共类MainActivity扩展了活动{
专用选项卡主机选项卡;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
tabs=(TabHost)findviewbyd(R.id.TabHost);
tabs.setup();
TabHost.TabSpec Favorit=tabs.newTabSpec(“Favorit”);
setContent(R.id.Favorit);
setIndicator((Build.VERSION.SDK_INT>10)?“Favorit”:“”);
tabs.addTab(Favorit);
TabHost.TabSpec All_line=tabs.newTabSpec(“全部”);
所有设置内容(R.id.All);
All_line.setIndicator((Build.VERSION.SDK_INT>10)?“All”:“”;
tabs.addTab(所有行);
对于(int i=0;i
framgment\u 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>


我希望我能帮忙

谢谢!然而,TabHosts可以在android2.3中实现吗(这就是我试图编译的内容)?相反,是否可以使用ActionBarSherlock做同样的事情?