我想让用户在选择新标签之前确认(通过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();
}
但是,如果用户想要选择"否" (不要切换标签),图书馆似乎已经选中了它?是否可以控制在库选择此新选项卡之前发生的事情,然后在实际切换之前请求用户确认?
谢谢!
答案 0 :(得分:3)
使用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>
我希望我能帮忙!