Android 自訂對話方塊使用靜態Handler傳遞參數

來源:互聯網
上載者:User

標籤:

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>

 

Android 自訂對話方塊使用靜態Handler傳遞參數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.