android 自定义 dialog


android 自定义 dialog
 

 

 

 

package com.curiousby.baoyou.cn.quote.dialog;
 

import com.curiousby.baoyou.cn.mysqlconsole.R; 
import com.curiousby.baoyou.cn.mysqlconsole.util.KeyBoardUtils;

import android.content.Context; 
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

/**
 * @author baoyou E-mail:curiousby@163.com
 * @version 2016-8-16 下午4:43:17
 *
 * desc: ...
 */
public class AddDatabaseDialog extends BaseDialog  implements OnClickListener{
	
	private Context mContext;
	
	private EditText mIpEditView;
	private EditText mDatabaseEditView;
	private EditText mUserNameEditView;
	private EditText mPasswordEditView;
	private TextView mConfirmView;
	private TextView mCancelView;

	private OnConfirmListener mOnConfirmListener;
	private OnCancleListener mOnCancleListener;
	
	public AddDatabaseDialog(Context context,OnConfirmListener onConfirmListener,OnCancleListener onCancleListener) {
		super(context,R.style.Theme_Light_AddDatabaseDialog); 
		this.mContext = context;
		this.mOnCancleListener = onCancleListener;
		this.mOnConfirmListener= onConfirmListener;
		setContentView(R.layout.dialog_add_database);
		
		initView();
		initListener();
	}
  

	private void initView() { 
		mIpEditView = (EditText) this.findViewById(R.id.et_dialog_add_database_ip);
		mDatabaseEditView = (EditText) this.findViewById(R.id.et_dialog_add_database_database_name);
		mUserNameEditView = (EditText) this.findViewById(R.id.et_dialog_add_database_database_username);
		mPasswordEditView = (EditText) this.findViewById(R.id.et_dialog_add_database_database_password);
		
		mConfirmView = (TextView) this.findViewById(R.id.tv_dialog_add_database_ok);
		mCancelView = (TextView) this.findViewById(R.id.tv_dialog_add_database_cancel);
	}
	private void initListener() { 
		mConfirmView.setOnClickListener(this);
		mCancelView.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) { 
		switch (view.getId()) {
		case R.id.tv_dialog_add_database_ok:
			mOnConfirmListener.onConfirm(new AddDatabaseDialogEntity(
					mIpEditView.getText().toString(),
					mDatabaseEditView.getText().toString(),
					mUserNameEditView.getText().toString(),
					mPasswordEditView.getText().toString()
					));
			KeyBoardUtils.closeKeybord(mIpEditView, mContext);
			KeyBoardUtils.closeKeybord(mDatabaseEditView, mContext);
			KeyBoardUtils.closeKeybord(mUserNameEditView, mContext);
			KeyBoardUtils.closeKeybord(mPasswordEditView, mContext);
			dismiss();
			break;
		case R.id.tv_dialog_add_database_cancel:
			mOnCancleListener.onCancle(null);
			KeyBoardUtils.closeKeybord(mIpEditView, mContext);
			KeyBoardUtils.closeKeybord(mDatabaseEditView, mContext);
			KeyBoardUtils.closeKeybord(mUserNameEditView, mContext);
			KeyBoardUtils.closeKeybord(mPasswordEditView, mContext);
			dismiss();
			break;
		default:
			break;
		}
	}

	public class AddDatabaseDialogEntity{
		public  String ip;
		public  String databaseName;
		public  String userName;
		public  String password;
		
		public AddDatabaseDialogEntity(String ip, String databaseName,
				String userName, String password) {
			this.ip = ip;
			this.databaseName = databaseName;
			this.userName = userName;
			this.password = password;
		}

		@Override
		public String toString() {
			return "AddDatabaseDialogEntity [ip=" + ip + ", databaseName="
					+ databaseName + ", userName=" + userName + ", password="
					+ password + "]";
		}
	}
}

 

 

 

package com.curiousby.baoyou.cn.mysqlconsole.activity;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.curiousby.baoyou.cn.mysqlconsole.R;
import com.curiousby.baoyou.cn.quote.dialog.AddDatabaseDialog;
import com.curiousby.baoyou.cn.quote.dialog.AddDatabaseDialog.AddDatabaseDialogEntity;
import com.curiousby.baoyou.cn.quote.dialog.BaseDialog.OnCancleListener;
import com.curiousby.baoyou.cn.quote.dialog.BaseDialog.OnConfirmListener;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick; 

@ContentView(R.layout.activity_main)
public class MainActivity extends Activity {

	public static final String TAG = "baoyou";
	private Context mContext;
	
	@ViewInject(R.id.iv_btn_menu)
	private ImageView addDatabaseImageView; 
	private AddDatabaseDialog addDatabaseDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		mContext = MainActivity.this;
		initView();
	}

	private void initView() {
		OnConfirmListener onConfirmListener = new OnConfirmListener() { 
			@Override
			public void onConfirm(Object obj) { 
				Log.e(TAG, "do save");
				Message msg = new Message(); 
				msg.what = SAVE_DATABASE_INFO; 
				msg.obj = obj;
				mHandler.sendMessage(msg);
			}
		};
		OnCancleListener onCancleListener = new OnCancleListener() {
			
			@Override
			public void onCancle(Object obj) { 
				Log.e(TAG, "donothing");
			}
		};
		addDatabaseDialog = new AddDatabaseDialog(mContext, onConfirmListener, onCancleListener);	
	}

	@OnClick({R.id.iv_btn_menu})
	public  void onclick(View view){
		switch (view.getId()) {
		case R.id.iv_btn_menu:
			addDatabaseDialog.show();
			break;
		default:
			break;
		}
	}

	static final int SAVE_DATABASE_INFO = 1;
	
	private Handler mHandler = new MyHandler(this);
	
	public static class MyHandler extends Handler { 
		WeakReference<MainActivity> mWR; 
		public MyHandler(MainActivity aActivity) {
			mWR = new WeakReference<MainActivity>(aActivity);
		} 
		 @Override
		public void handleMessage(android.os.Message msg) {
			 MainActivity activity = mWR.get();
			switch (msg.what) {
			case SAVE_DATABASE_INFO:

				if (msg.obj instanceof AddDatabaseDialogEntity) {
					AddDatabaseDialogEntity entity = (AddDatabaseDialogEntity)msg.obj;
					Log.i(TAG, ""+entity.toString());
					
					//TODO : SAVA entity
					//TODO: refresh listview
					Toast.makeText(activity,  ""+entity.toString(), Toast.LENGTH_LONG).show();
				}
				break;

			default:
				break;
			}
		};
	};
	
}

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="350dip"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/blue_main_color"
        android:gravity="center"
        android:padding="10dip"
        android:text="@string/dialog_add_database_head_title"
        android:textColor="@color/white"
        android:textSize="@dimen/title_size" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dip"
        android:background="@color/gray" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="@string/dialog_add_database_ip"
        android:textColor="@color/gray"
        android:textSize="@dimen/desc_size" />

    <EditText
        android:id="@+id/et_dialog_add_database_ip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dip"
        android:layout_marginRight="1dip"
        android:layout_marginTop="2dip"
        android:background="@drawable/shap_rect_bg_white"
        android:focusable="true"
        android:inputType=""
        android:padding="8dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="@string/dialog_add_database_database_name"
        android:textColor="@color/gray"
        android:textSize="@dimen/desc_size" />

    <EditText
        android:id="@+id/et_dialog_add_database_database_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dip"
        android:layout_marginRight="1dip"
        android:layout_marginTop="2dip"
        android:background="@drawable/shap_rect_bg_white"
        android:focusable="true"
        android:inputType=""
        android:padding="8dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="@string/dialog_add_database_username"
        android:textColor="@color/gray"
        android:textSize="@dimen/desc_size" />

    <EditText
        android:id="@+id/et_dialog_add_database_database_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dip"
        android:layout_marginRight="1dip"
        android:layout_marginTop="2dip"
        android:background="@drawable/shap_rect_bg_white"
        android:focusable="true"
        android:inputType=""
        android:padding="8dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="@string/dialog_add_database_password"
        android:textColor="@color/gray"
        android:textSize="@dimen/desc_size" />

    <EditText
        android:id="@+id/et_dialog_add_database_database_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dip"
        android:layout_marginRight="1dip"
        android:layout_marginTop="2dip"
        android:background="@drawable/shap_rect_bg_white"
        android:focusable="true"
        android:inputType="textPassword"
        android:padding="8dip" />

    <TextView
        android:id="@+id/tv_dialog_add_database_ok"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="15dip"
        android:background="@drawable/selector_btn_confirm"
        android:gravity="center_horizontal"
        android:paddingBottom="10dip"
        android:paddingTop="10dip"
        android:text="@string/dialog_add_database_ok"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/tv_dialog_add_database_cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="15dip"
        android:layout_marginBottom="15dip"
        android:background="@drawable/selector_btn_confirm"
        android:gravity="center_horizontal"
        android:paddingBottom="10dip"
        android:paddingTop="10dip"
        android:text="@string/dialog_add_database_cancel"
        android:textColor="@color/white" />

</LinearLayout>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。


android 自定义 dialogandroid 自定义 dialogandroid 自定义 dialog
 
 
 谢谢您的赞助,我会做的更好!

 

 

上一篇:Midway Serverless 发布 2.0,一体化让前端研发再次提效


下一篇:经典技术文章翻译(1):COM+集成:.NET Enterprise Services 如何帮你建立分布式应用(2)