在 Android 选项卡布局中显示进度对话框
我需要在单击选项卡时显示进度对话框,
我的代码 ID
public class SIPTabWidget extends TabActivity{
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
final Resources res = getResources(); // Resource object to get Drawables
final TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ListContacts.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost
.newTabSpec("contacts")
.setIndicator("",
res.getDrawable(R.drawable.tab_contacts))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Dialer.class);
spec = tabHost
.newTabSpec("dialer")
.setIndicator("", res.getDrawable(R.drawable.tab_dial))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Favorites.class);
spec = tabHost
.newTabSpec("favorites")
.setIndicator("", res.getDrawable(R.drawable.tab_favorites))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, org.sipdroid.sipua.ui.Settings.class);
spec = tabHost
.newTabSpec("settings")
.setIndicator("", res.getDrawable(R.drawable.tab_settings))
.setContent(intent);
tabHost.addTab(spec);
}
当我单击设置选项卡时,
final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please wait..", "Doing stuff..", true);
dialog.setCancelable(false);
dialog.show();
,调用设置活动,在其 oncreate 中我给出了
对我来说,在显示“设置”布局之前,它需要一些时间来加载布局,因此在此之前我需要显示“进度对话框”,这意味着当我单击“设置”选项卡时< /strong> 我需要在以下情况下关闭对话框该选项卡更改为 Settings
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
System.out.println(" Tab ID: "+tabId);
if(tabId.equals("dial")){
dialog .dismiss();
}
}
});
Settings.class
public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener, OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("Settings oncreate");
final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please wait..", "Doing stuff..", true);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Thread t = new Thread() {
public void run() {
//send message
handler.sendEmptyMessage(0);
}
};
if (Receiver.mContext == null) Receiver.mContext = this;
addPreferencesFromResource(R.xml.preferences);
setDefaultValues();
Codecs.check();
t.start
}
I need to show a progress dialog, while clicking a tab,
My code id
public class SIPTabWidget extends TabActivity{
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
final Resources res = getResources(); // Resource object to get Drawables
final TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ListContacts.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost
.newTabSpec("contacts")
.setIndicator("",
res.getDrawable(R.drawable.tab_contacts))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Dialer.class);
spec = tabHost
.newTabSpec("dialer")
.setIndicator("", res.getDrawable(R.drawable.tab_dial))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Favorites.class);
spec = tabHost
.newTabSpec("favorites")
.setIndicator("", res.getDrawable(R.drawable.tab_favorites))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, org.sipdroid.sipua.ui.Settings.class);
spec = tabHost
.newTabSpec("settings")
.setIndicator("", res.getDrawable(R.drawable.tab_settings))
.setContent(intent);
tabHost.addTab(spec);
}
when i click the settings tab, Setting activity is called, in its oncreate i gave
final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please wait..", "Doing stuff..", true);
dialog.setCancelable(false);
dialog.show();
for me before showing the Settings layout its taking some time to load the layout, so before that i need to show the ProgressDialog, means the time when i click the Settings tab and i need to dismiss the Dialog when the tab changes to Settings
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
System.out.println(" Tab ID: "+tabId);
if(tabId.equals("dial")){
dialog .dismiss();
}
}
});
Settings.class
public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener, OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("Settings oncreate");
final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please wait..", "Doing stuff..", true);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Thread t = new Thread() {
public void run() {
//send message
handler.sendEmptyMessage(0);
}
};
if (Receiver.mContext == null) Receiver.mContext = this;
addPreferencesFromResource(R.xml.preferences);
setDefaultValues();
Codecs.check();
t.start
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,每个选项卡都是一个活动...我想
花费时间
在设置活动中...将其放入设置活动中:
记住:在您< code>run() 在此块中
如果需要更改 UI,则必须使用
Well each tab is an activity... I suppose that
what is taking time
is in the settings activity...Put this in the setting activity:
Remember: In you
run()
in this blockIf anything needs to change the UI, you must do it using
你可以使用android的asyntask功能来显示进度对话框参考这个http://developer .android.com/reference/android/os/AsyncTask.html
you can use the asyntask feature of android to show Progress dialog refer this http://developer.android.com/reference/android/os/AsyncTask.html
请参阅我的答案,
“旋转从 SD 卡删除文件夹时的“滚轮”进度对话框
,或者您必须执行异步任务才能执行此操作。
Refer to my answer here,
"Rotating wheel" progress dialog while deleting folder from SD card
or you must go for async task to do this.
如果 getParent() 不适合您,请尝试仅使用
TabsActivity.context
(或替换父选项卡活动类的名称)。我正在使用嵌套活动,因此使用 getParent() 仍然没有返回对话框的正确上下文。在尝试了上述建议的 20 种不同变体之后,我将这一行替换为:
With:
,它的效果就像一个魅力。您还需要在 TabsActivity 类中创建上下文变量。类似于 onCreate 方法中的
public static TabsActivity context;
和context=this
。If getParent() doesn't work for you, try using just
TabsActivity.context
(or substitute the name of your parent tab activity class). I am using nested activities and as a result using getParent() is still not returning the right context for the dialog.After trying 20 different variations of the suggestions above I replaced this line:
With:
and it worked like a charm. You'll also need to create the context variable in the TabsActivity class. Something like
public static TabsActivity context;
andcontext=this
in the onCreate method.