弹出对话框,内容显示随机的自定义内容:
public class MyProgressDialog extends Dialog { Context context; String title; TextView tv_title,tv_content; Handler handler; public MyProgressDialog(Context context,String title) { super(context,R.style.progress_Dialog); // TODO Auto-generated constructor stub this.context = context; this.title = title; } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); int width = PublicUtil.getWidth(context); setContentView(R.layout.dialog); LinearLayout ll_root = (LinearLayout) findViewById(R.id.ll_root); LayoutParams lp = (LayoutParams) ll_root.getLayoutParams(); lp.width = width*2/3; ll_root.setLayoutParams(lp); tv_content = (TextView) findViewById(R.id.tv_content); if(title!=null){ tv_title = (TextView) findViewById(R.id.tv_title); tv_title.setText(title); } String data = PublicUtil.getSharedValue(context, "tips", "tips", context.getString(R.string.tips)); final ArrayList<Tip> tips = PublicUtil.parserJsonList(data, "tips", Tip.class); handler = new Handler(){ public void handleMessage(android.os.Message msg) { tv_content.setText("小贴士:"+tips.get(msg.what).getContent()); handler.sendEmptyMessageDelayed((int)(Math.random()*tips.size()), 5000); }; }; handler.sendEmptyMessage((int)(Math.random()*tips.size())); } public void setTitle(String title) { this.title = title; tv_title.setText(title); } }
<style name="progress_Dialog" parent="@android:Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@drawable/none</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>
调用: myProgressDialog = new MyProgressDialog(this, “我的标题,可传null”); myPro...show(); myPro...dismiss()