zoukankan      html  css  js  c++  java
  • Android 自定义对话框使用静态Handler传递参数

    JsdMainDialog.java

    package com.jsd.demo;
    
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    /**
     *
     * @author jiangshide
     *
     */
    public class JsdMainDialog extends Activity {
     
     private Context mContext;
     
     private Button mSub;
     private TextView mResultValue;
     
     public static Handler handler = new Handler();
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            getViewById();
        }
       
        public void getViewById(){
         mContext = this;
         mSub = (Button) this.findViewById(R.id.sub);
         mResultValue = (TextView) this.findViewById(R.id.resultValue);
         mResultValue.setText("没有通过Handler处理");
         mSub.setOnClickListener(listener);
        }
       
        private OnClickListener listener = new OnClickListener() {
      @Override
      public void onClick(View v) {
       switch (v.getId()) {
       case R.id.sub:
        final String flag = "rqbackvalue";
        DialogCustomer  dc = new DialogCustomer(mContext,flag);
        dc.show();
        handler = new Handler(){
         public void handleMessage(Message msg) {
          String resultFlag = msg.getData().getString("flags");
          if(resultFlag.equalsIgnoreCase(flag)){
           mResultValue.setText("这是通过Handler处理过后来显示数据的");
           mResultValue.setTextColor(Color.CYAN);
           mResultValue.setTextSize(30);
          }
         };
        };
        break;
    
       default:
        break;
       }
      }
     };
    }
    
     

    DialogCustomer.java:

    package com.jsd.demo;
    
    import android.app.Dialog;
    import android.content.Context;
    import android.os.Bundle;
    import android.os.Message;
    import android.view.View;
    import android.widget.Button;
    
    /**
     *
     * @author jiangshide
     *
     */
    public class DialogCustomer extends Dialog {
    
     private Context mContext;
     
     private Button ok;
     String flag;
     
     public DialogCustomer(Context c,String flag) {
      super(c);
      this.mContext = c;
      this.flag = flag;
     }
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.dialog);
      getViewById();
     }
     
     public void getViewById(){
      ok = (Button) this.findViewById(R.id.dialog_ok);
      ok.setOnClickListener(listener);
     }
     
     private android.view.View.OnClickListener listener = new android.view.View.OnClickListener() {
      @Override
      public void onClick(View v) {
       switch (v.getId()) {
       case R.id.dialog_ok:
        Message msg = new Message();//获取设置一个信息保存点
        msg.what=1;
        msg.getData().putString("flags", flag);
        JsdMainDialog.handler.sendMessage(msg);//把数据放进LOOPER队列里
        dismiss();
        break;
       }
      }
     };
    }

    dialog.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      >
      <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="当返回时把参数传递过去并执行判断后的操作"
       />
      <Button
       android:id="@+id/dialog_ok"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" 
       android:text="请点击"
       />
    </LinearLayout>
  • 相关阅读:
    Step by step Dynamics CRM 2013安装
    SQL Server 2012 Managed Service Account
    Step by step SQL Server 2012的安装
    Step by step 活动目录中添加一个子域
    Step by step 如何创建一个新森林
    向活动目录中添加一个子域
    活动目录的信任关系
    RAID 概述
    DNS 正向查找与反向查找
    Microsoft Dynamics CRM 2013 and 2011 Update Rollups and Service Packs
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4262067.html
Copyright © 2011-2022 走看看