一.自定义可点击spiner控件的源码:
package com.myprojrect.android.widget;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.widget.Spinner;
public class ClickControlledSpinner extends Spinner{
public ClickControlledSpinner(Context context) {
super(context);
}
public ClickControlledSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ClickControlledSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 实现点击方法
* */
private void onClick() {
if (onClickMyListener != null && isEnabled()) {
onClickMyListener.onClick();
}
}
private OnClickMyListener onClickMyListener;
/**
* 注册自定义的点击事件监听
* Register the click event self-fulfilling listener.
* @param onClickMyListener
*/
public void setOnClickMyListener(OnClickMyListener onClickMyListener) {
this.onClickMyListener = onClickMyListener;
}
/**
* 自定义点击事件监听. Click event self-fulfilling listener.
*/
public interface OnClickMyListener {
/**
* 点击时触发 警告:该方法在非UI线程中执行
*
* Triggers when click event occurs. Warning: this method does not run
* in UI thread.
*/
public void onClick();
}
}
二.相关布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/user_login_title_tv"
android:text="@string/recommend_tag"
android:textSize="@dimen/font_size_4"
android:textColor="@color/text_channel_item"
android:textStyle="bold"
android:paddingLeft="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<View
android:layout_marginTop="10dip"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="@color/line_color"/>
<LinearLayout
android:id="@+id/login_content_view"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="60dip"
android:paddingRight="60dip">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="15dip">
<TextView
android:text="@string/recommend_mobile_phone_tag"
android:textSize="@dimen/font_size_3"
android:textColor="@color/text_channel_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/recommend_mobile_phone_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_marginLeft="5dip"
android:layout_weight="1"
android:digits="0123456789+*#"
android:maxLength="20"
android:hint="@string/recommend_mobile_phone_hint_tag"
android:textSize="@dimen/font_size_2"
android:textColor="@color/text_channel_item"
android:textCursorDrawable="@null"
android:background="@drawable/edit_bg"/>
<ImageView
android:id="@+id/recommend_add_contact_but"
android:src="@drawable/btn_add_contact_bg"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="@string/recommend_mobile_phone_tag"
android:textSize="@dimen/font_size_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
<TextView
android:text="@string/recommend_mobile_phone_tip_tag"
android:textSize="@dimen/font_size_1"
android:textColor="@color/text_channel_item"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/recommend_message_lay"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="15dip">
<TextView
android:text="@string/recommend_message_tag"
android:textSize="@dimen/font_size_3"
android:textColor="@color/text_channel_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.myprojrect..android.widget.ClickControlledSpinner
android:id="@+id/recommend_message_spinner_sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:spinnerMode="dropdown"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:popupBackground="@drawable/drop_down"
android:background="@drawable/spinner_bg"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:gravity="center"
android:weightSum="10">
<Button
android:id="@+id/recommend_send_submit_but"
android:text="@string/recommend_send_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_3"
android:layout_gravity="center"
style="@style/CustomButton"/>
<View
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="1"/>
<Button
android:id="@+id/recommend_cancel_but"
android:text="@string/recommend_cancel_tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size_3"
android:layout_gravity="center"
style="@style/CustomButton"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
三.调用的对话框
/**
* 显示下拉列表的对话框
*
*/
public static void showReCommendDailog(final Activity context, final String contentID, final String contentName) {
final Dialog dialog = createLoginDialog(context,R.layout.recommend_layout);
final EditText mobile_phone_et = (EditText) dialog.findViewById(R.id.recommend_mobile_phone_et);
final ClickControlledSpinner recommend_message_spinner_sp = (ClickControlledSpinner) dialog.findViewById(R.id.recommend_message_spinner_sp);
final View recommend_add_contact_but = dialog.findViewById(R.id.recommend_add_contact_but);
final Button recommend_send_submit_but = (Button) dialog.findViewById(R.id.recommend_send_submit_but);
final Button recommend_cancel_but = (Button) dialog.findViewById(R.id.recommend_cancel_but);
final String [] recommendDataStrings = context.getResources().getStringArray(R.array.recommended_items);
recommend_message_spinner_sp.setAdapter(new ArrayAdapter<String>(context, R.layout.setting_spinner_item,recommendDataStrings));
recommend_message_spinner_sp.setOnClickMyListener(new OnClickMyListener() {
@Override
public void onClick() {
//关闭输入法
InputMethodManager imm = ((InputMethodManager)context.getSystemService(context.INPUT_METHOD_SERVICE));
imm.hideSoftInputFromWindow(mobile_phone_et.getWindowToken(), 0);
}
});
recommend_message_spinner_sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//关闭输入法
InputMethodManager imm = ((InputMethodManager)context.getSystemService(context.INPUT_METHOD_SERVICE));
imm.hideSoftInputFromWindow(mobile_phone_et.getWindowToken(), 0);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
//关闭输入法
InputMethodManager imm = ((InputMethodManager)context.getSystemService(context.INPUT_METHOD_SERVICE));
imm.hideSoftInputFromWindow(mobile_phone_et.getWindowToken(), 0);
}
});
onGetContactReturn = new MyRunnable(){
@Override
public void run(String data) {
if(data == null)return;
mobile_phone_et.setText(data);
}
};
recommend_add_contact_but.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
context.startActivityForResult(intent, PICK_CONTACT);
}
});
dialog.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
onGetContactReturn = null;
}
});
recommend_send_submit_but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String phoneStr = mobile_phone_et.getText().toString().trim();
String contentTemp = recommend_message_spinner_sp.getSelectedItem().toString();
if (contentTemp.equals(recommendDataStrings[1])) {
contentTemp = recommend_self_message_et.getText().toString().trim();
}
final String contenStr = contentTemp;
if (TextUtils.isEmpty(phoneStr)) {
ToastUtil.showToast(context, R.string.dialog_recommend_empty_tip);
} else if (TextUtils.isEmpty(contenStr)) {
ToastUtil.showToast(context, R.string.dialog_recommend_content_empty_tip);
} else {
if (!isChinaTelecomNumber(phoneStr)) {
ToastUtil.showToast(context, context.getString(R.string.dialog_recommend_phone_err, phoneStr));
return;
}
final Dialog waittingDialog = DialogUtil.getNetDialog(context,false);
waittingDialog.show();
new Thread() {
@Override
public void run() {
final boolean isSuccess = DataSaxParser
.getInstance(context).recommendedContent(
contentID, null, phoneStr,
contenStr);
context.runOnUiThread(new Runnable() {
@Override
public void run() {
if (waittingDialog != null) {
waittingDialog.dismiss();
}
if (isSuccess) {
ToastUtil.showToast(context,
R.string.recommeded_success);
dialog.dismiss();
} else {
ToastUtil.showToast(context, R.string.recommend_failure);
}
}
});
}
}.start();
}
}
});
recommend_cancel_but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
四.总结:
1.在自定义ClickControlledSpinner里面声明一个接口
/**
* 自定义点击事件监听. Click event self-fulfilling listener.
*/
public interface OnClickMyListener {
/**
* 点击时触发 警告:该方法在非UI线程中执行
*
* Triggers when click event occurs. Warning: this method does not run
* in UI thread.
*/
public void onClick();
}
2.在自定义ClickControlledSpinner里面定义注册一个自定义的点击事件监听事件(使用一个回调函数):
private OnClickMyListener onClickMyListener;
/**
* 注册自定义的点击事件监听
* Register the click event self-fulfilling listener.
* @param onClickMyListener
*/
public void setOnClickMyListener(OnClickMyListener onClickMyListener) {
this.onClickMyListener = onClickMyListener;
}
3.在自定义ClickControlledSpinner里面实现接口OnClickMyListener里面的 onClick()方法:
/**
* 实现点击方法
* */
private void onClick() {
if (onClickMyListener != null && isEnabled()) {
onClickMyListener.onClick();
}
}
4.在xml文件中引入自定义spinner控件:
<com.myprojrect..android.widget.ClickControlledSpinner
android:id="@+id/recommend_message_spinner_sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:spinnerMode="dropdown"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:popupBackground="@drawable/drop_down"
android:background="@drawable/spinner_bg"/>
5.在代码中引用自定义spinner控件:
final ClickControlledSpinner recommend_message_spinner_sp = (ClickControlledSpinner) dialog.findViewById(R.id.recommend_message_spinner_sp);
6.在代码中调用自定义spinner控件的自定义点击事件:
recommend_message_spinner_sp.setOnClickMyListener(new OnClickMyListener() {
@Override
public void onClick() {
//可以根据需要实现相关操作
//关闭输入法
InputMethodManager imm = ((InputMethodManager)context.getSystemService(context.INPUT_METHOD_SERVICE));
imm.hideSoftInputFromWindow(mobile_phone_et.getWindowToken(), 0);
}
});