zoukankan      html  css  js  c++  java
  • Android 4 学习(21):对话框

    对话框

    创建Dialog的两种方式:

    1. 使用Dialog类或其子类,包括DialogFragment

    2. Activity中使用Dialog主题(theme

    下面是使用Dialog类的一个例子:

     

    // Create the new Dialog.
    Dialog dialog = new Dialog(MyActivity.this);
    // Set the title. dialog.setTitle(“Dialog Title”); // Inflate the layout. dialog.setContentView(R.layout.dialog_view); // Update the Dialog’s contents.
    TextView text = (TextView)dialog.findViewById(R.id.dialog_text_view);
    text.setText(“This is the text in my dialog”);
    // Display the Dialog. dialog.show();

     

    也可以使用Dialog的子类,例如常用的AlertDialog

     

    Context context = MyActivity.this;
    String title = “It is Pitch Black”;
    String message = “You are likely to be eaten by a Grue.”;
    String button1String = “Go Back”;
    String button2String = “Move Forward”; AlertDialog.Builder ad = new AlertDialog.Builder(context);
    ad.setTitle(title);ad.setMessage(message); ad.setPositiveButton(button1String,
    new DialogInterface.OnClickListener() {   public void onClick(DialogInterface dialog, int arg1) {     eatenByGrue();   } }); ad.setNegativeButton(button2String, new DialogInterface.OnClickListener(){   public void onClick(DialogInterface dialog, int arg1) {     //do nothing   } });

     

    还可以设置是否显示Cancel按钮:

     

    ad.setCancelable(true); 
    ad.setOnCancelListener(new DialogInterface.OnCancelListener() { 
      public void onCancel(DialogInterface dialog) { 
        eatenByGrue();   }
    });

     

    使用内置的Dialog

    Android中提供了这些现成的Dialog供我们使用:

    1. CharacterPickerDialog 

    2. DatePickerDialog 

    3. TimePickerDialog 

    4. ProgressDialog 

    使用Fragment

    Android中几乎所有的可见部分都可以放到Fragment中,我认为Fragment相对于View的一大优点是可以控制自己的生命周期。

     

    public class MyDialogFragment extends DialogFragment {
      private static String CURRENT_TIME = “CURRENT_TIME”; 
      public static MyDialogFragment newInstance(String currentTime) {
        // Create a new Fragment instance with the specified parameters.
        MyDialogFragment fragment = new MyDialogFragment();
        Bundle args = new Bundle();     args.putString(CURRENT_TIME, currentTime);     fragment.setArguments(args);     return fragment;   }
    }

     

     

     

     

     

  • 相关阅读:
    CSS+js弹出居中的背景半透明div层
    多服务器Remoting
    【Matlab图像处理】学习笔记:二值图片
    [转]各种颜色相互转换算法的C语言源代码
    [转]Xilinx:ERROR:Xst:2035
    modelsim仿真xilinx IP DCM
    [转]MATLAB out of memory
    [转]解决Xilinx Platform Studio无法打开 设置 环境变量
    【Matlab图像处理】学习笔记:读取16进制RGB文档转为彩色图片
    【Matlab图像处理】学习笔记:提取图片的R,G,B分量
  • 原文地址:https://www.cnblogs.com/jubincn/p/3522541.html
Copyright © 2011-2022 走看看