Android 封裝實現各種樣式對話方塊

來源:互聯網
上載者:User

標籤:false   tail   close   ems   contex   keyevent   keycode   post   empty   

先 


實現代碼

package com.jock.alertdialog;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;import android.widget.Toast;/** * 精仿iOSAlertViewController控制項Demo */public class MainActivity extends Activity implements OnItemClickListener, OnDismissListener {    private AlertView mAlertView;//避免建立反覆View。先建立View,然後須要的時候show出來,推薦這個做法    private AlertView mAlertViewExt;//表單拓展範例    private EditText etName;//拓展View內容    private InputMethodManager imm;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);        mAlertView = new AlertView("標題", "內容", "取消", new String[]{"確定"}, null, this, AlertView.Style.Alert, this).setCancelable(true).setOnDismissListener(this);        //拓展表單        mAlertViewExt = new AlertView("提示", "請完好你的設定檔。", "取消", null, new String[]{"完畢"}, this, AlertView.Style.Alert, this);        ViewGroup extView = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.alertext_form,null);        etName = (EditText) extView.findViewById(R.id.etName);        etName.setOnFocusChangeListener(new View.OnFocusChangeListener() {            @Override            public void onFocusChange(View view, boolean focus) {                //輸入框出來則往上移動                boolean isOpen=imm.isActive();                mAlertViewExt.setMarginBottom(isOpen&&focus ?

120 :0); System.out.println(isOpen); } }); mAlertViewExt.addExtView(extView); } public void alertShow1(View view) { mAlertView.show(); } public void alertShow2(View view) { new AlertView("標題", "內容", null, new String[]{"確定"}, null, this, AlertView.Style.Alert, this).show(); } public void alertShow3(View view) { new AlertView(null, null, null, new String[]{"高亮button1", "高亮button2", "高亮button3"}, new String[]{"其它button1", "其它button2", "其它button3", "其它button4", "其它button5", "其它button6", "其它button7", "其它button8", "其它button9", "其它button10", "其它button11", "其它button12"}, this, AlertView.Style.Alert, this).show(); } public void alertShow4(View view) { new AlertView("標題", null, "取消", new String[]{"高亮button1"}, new String[]{"其它button1", "其它button2", "其它button3"}, this, AlertView.Style.ActionSheet, this).show(); } public void alertShow5(View view) { new AlertView("標題", "內容", "取消", null, null, this, AlertView.Style.ActionSheet, this).setCancelable(true).show(); } public void alertShow6(View view) { new AlertView("上傳頭像", null, "取消", null, new String[]{"拍照", "從相冊中選擇"}, this, AlertView.Style.ActionSheet, this).show(); } public void alertShowExt(View view) { mAlertViewExt.show(); } private void closeKeyboard() { //關閉軟鍵盤 imm.hideSoftInputFromWindow(etName.getWindowToken(),0); //恢複位置 mAlertViewExt.setMarginBottom(0); } @Override public void onItemClick(Object o,int position) { closeKeyboard(); //推斷是否是拓展表單View。並且點擊的是非取消button if(o == mAlertViewExt && position != AlertView.CANCELPOSITION){ String name = etName.getText().toString(); if(name.isEmpty()){ Toast.makeText(this, "啥都沒填呢", Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(this, "hello,"+name, Toast.LENGTH_SHORT).show(); } return; } Toast.makeText(this, "點擊了第" + position + "個", Toast.LENGTH_SHORT).show(); } @Override public void onDismiss(Object o) { closeKeyboard(); Toast.makeText(this, "消失了", Toast.LENGTH_SHORT).show(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { if(mAlertView!=null && mAlertView.isShowing()){ mAlertView.dismiss(); return false; } } return super.onKeyDown(keyCode, event); }}


點擊下載原始碼



Android 封裝實現各種樣式對話方塊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.