安卓常用模块收集(一)--编辑窗口

2016/09/23//

//by   xbw


最近发现安卓里的好多模块都能重复使用,避免麻烦收集一下常用的布局等



如图,编辑模块,我们在修改个人信息时,大多会需要这个,,,,

先上布局文件

edit_text_info_window.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vBaseBottomWindowRoot"
    style="@style/bottom_window_page" >

    <RelativeLayout
        style="@style/topbar_bg"
        android:background="@color/white" >

        <TextView
            android:id="@+id/tvEditTextInfoTitle"
            style="@style/topbar_title"
            android:layout_centerHorizontal="true"
            android:text="编辑"
            android:textColor="@color/black"
            android:textSize="20sp" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/common_item_distance"
        android:background="@color/white" />

    <TextView
        android:id="@+id/tvEditTextInfoPlace"
        style="@style/text_middle_blue"
        android:layout_width="match_parent"
        android:background="@drawable/bg_white_to_gray"
        android:gravity="center_vertical|left"
        android:hint="点击选择地址"
        android:paddingBottom="12dp"
        android:paddingLeft="@dimen/common_item_left_tv_padding"
        android:paddingRight="@dimen/common_item_right_img_padding_right"
        android:paddingTop="6dp"
        android:visibility="gone" />

    <LinearLayout style="@style/edit_item" >

        <LinearLayout
            style="@style/edit_item_right_et"
            android:layout_marginLeft="@dimen/common_item_left_tv_padding"
            android:layout_weight="1"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/etEditTextInfo"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="3dp"
                android:layout_weight="1"
                android:background="@null"
                android:cursorVisible="true"
                android:gravity="center|left"
                android:hint="请在这里输入"
                android:maxLength="30"
                android:paddingRight="10dp"
                android:textColor="@color/black"
                android:textCursorDrawable="@drawable/edit_cusor"
                android:textSize="17sp" >

                <requestFocus />
            </EditText>

            <View
                style="@style/divider_horizontal_1dp"
                android:background="@color/green" />
        </LinearLayout>

        <ImageView
            android:id="@+id/ivEditTextInfoClear"
            style="@style/edit_item_right_clear"
            android:layout_width="wrap_content"
            android:visibility="gone" />
    </LinearLayout>

    <TextView
        android:id="@+id/tvEditTextInfoRemind"
        style="@style/content_item_remind_tv"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:paddingBottom="30dp"
        android:paddingTop="@dimen/common_item_padding_bottom"
        android:text="您还可以输入" />

    <View style="@style/divider_horizontal_1px_gray" />

    <LinearLayout
        style="@style/ll_horizontal_match_wrap"
        android:background="@color/white" >

        <TextView
            android:id="@+id/tvEditTextInfoReturn"
            style="@style/bottom_menu_btn_cancel"
            android:layout_weight="1" />

        <View style="@style/divider_vertical_1px_gray" />

        <TextView
            android:id="@+id/tvEditTextInfoForward"
            style="@style/bottom_menu_btn_save"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

styles.xml
<style name="activity_page">
        <item name="android:splitMotionEvents">false</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:background">@color/activity_bg</item>
        <item name="android:orientation">vertical</item>
    </style>
    <style name="bottom_window_page" parent="activity_page">
        <item name="android:gravity">bottom|center_horizontal</item>
        <item name="android:background">@color/alpha_complete</item>
    </style>
    <style name="topbar_bg">
        <item name="android:background">@color/topbar_bg</item>
        <item name="android:gravity">center_vertical</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">@dimen/topbar_height</item>
    </style>
    <style name="topbar_title">
        <item name="android:textColor">@color/black</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:textSize">18sp</item>
    </style>
    <style name="wrap_wrap">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
    <style name="text" parent="wrap_wrap">
        <item name="android:gravity">center</item>
        <item name="android:textColor">@color/gray_3</item>
    </style>
    <style name="text_middle" parent="text">
        <item name="android:textSize">@dimen/text_size_middle</item>
    </style>
    <style name="text_middle_blue" parent="text_middle">
        <item name="android:textColor">@color/blue</item>
    </style>
    <style name="edit_item">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">50dp</item>
        <item name="android:background">@color/white_selected</item>
        <item name="android:gravity">center_vertical</item>
        <item name="android:orientation">horizontal</item>
    </style>
    <style name="edit_item_right_et" parent="edit_item">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:background">@null</item>
        <item name="android:gravity">center|left</item>
        <item name="android:paddingRight">10dp</item>
        <item name="android:maxLength">24</item>
        <item name="android:singleLine">true</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:textSize">17sp</item>
    </style>
    <style name="divider_horizontal_1dp" parent="divider">
        <item name="android:layout_height">1dp</item>
        <item name="android:layout_width">fill_parent</item>
    </style>
    <style name="edit_item_right_clear">
        <item name="android:layout_width">30dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:src">@drawable/cancel_light</item>
        <item name="android:textSize">@dimen/text_size_middle</item>
        <item name="android:textColor">@color/gray</item>
        <item name="android:gravity">center</item>
        <item name="android:paddingRight">@dimen/common_item_padding_right</item>
    </style>
    <style name="content_item_remind_tv">
        <item name="android:layout_height">@dimen/common_item_remind_tv_height</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:textSize">14sp</item>
        <item name="android:textColor">@color/gray_1</item>
        <item name="android:gravity">center_vertical</item>
        <item name="android:paddingLeft">@dimen/common_item_left_tv_padding</item>
        <item name="android:paddingRight">@dimen/common_item_right_img_padding_right</item>
    </style>
    <style name="divider_horizontal_1px" parent="divider">
        <item name="android:layout_height">1px</item>
        <item name="android:layout_width">fill_parent</item>
    </style>
    <style name="divider_horizontal_1px_gray" parent="divider_horizontal_1px">
        <item name="android:background">@color/gray_2</item>
    </style>
    <style name="match_wrap">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
    <style name="ll_horizontal_match_wrap" parent="match_wrap">
        <item name="android:gravity">center_vertical</item>
        <item name="android:orientation">horizontal</item>
    </style>

    <style name="bottom_menu_btn_cancel" parent="bottom_menu_btn">
        <item name="android:text">取消</item>
        <item name="android:textColor">@color/gray_3</item>
    </style>
    <style name="divider">
        <item name="android:background">@color/alpha_3</item>
    </style>
    <style name="divider_vertical_1px" parent="divider">
        <item name="android:layout_width">1px</item>
        <item name="android:layout_height">fill_parent</item>
    </style>
    <style name="divider_vertical_1px_gray" parent="divider_vertical_1px">
        <item name="android:background">@color/gray_2</item>
    </style>
    <style name="bottom_menu_btn">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:padding">@dimen/btn_bottom_padding</item>
        <item name="android:background">@drawable/bg_item_to_alpha</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:gravity">center</item>
    </style>
    <style name="bottom_menu_btn_save" parent="bottom_menu_btn">
        <item name="android:text">保存</item>
    </style>

dimens.xml
 <!-- 分割线高度 -->
    <dimen name="line_height">1dp</dimen>
    <dimen name="topbar_height">50dp</dimen>
    <dimen name="common_item_distance">15dp</dimen>
    <dimen name="common_item_left_tv_padding">15dp</dimen>
    <dimen name="common_item_right_img_padding_right">15dp</dimen>
    <dimen name="text_size_middle">16sp</dimen>
    <dimen name="common_item_padding_right">12dp</dimen>
    <dimen name="common_item_remind_tv_height">30dp</dimen>
    <dimen name="common_item_padding_bottom">15dp</dimen>
    <dimen name="btn_bottom_padding">10dp</dimen>

colors.xml
 <!-- 控件按下时显示的颜色 -->
    <color name="list_backgroud_color">#2fb3cb</color>
    <color name="title_color">#0f8ec5</color>
    <color name="content_color">#737373</color>
    <color name="alpha_complete">#0000</color>
    <color name="topbar_bg">#33cc99</color>
    <color name="divider">#dddddd</color>
    <color name="white_selected">#ffffff</color>
    <color name="green">#33cc66</color>
    <color name="gray">#666666</color>
    <color name="gray_1">#b5b5b5</color>
    <color name="gray_2">#999999</color>
    <color name="gray_3">#666666</color>
    <color name="alpha_1">#1000</color>
    <color name="alpha_3">#3000</color>
    <color name="activity_bg">#f7f8f8</color>
    <drawable name="transparent_background">#00000000</drawable>
    <color name="black"> #000000</color>
    <color name="blue">#68217A</color>

drawable文件
bg_item_to_alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/alpha_1" android:state_pressed="true"/>
    <item android:drawable="@color/alpha_1" android:state_focused="true"/>

</selector>
bg_white_to_gray.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/divider" android:state_pressed="true"/>
    <item android:drawable="@color/white"/>

</selector>
edit_cusor.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/green" />
    <size android:width="1dp" />
</shape>
cancle_light.png图片资源


窗口封装起来了
根据自己的需要修改
package com.xbw.danmu.danmu;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Random;


public class ChangeDialog {
	private EditText edit;
	private TextView charnum_tx;
	private int charnum = 15;
	
	Context context;
	public ChangeDialog(Context context) {
		this.context=context;
	}
	
	public void showChangeDialog(String contexts) {
		LayoutInflater layoutInflater = LayoutInflater.from(context);
		LinearLayout layout = (LinearLayout) layoutInflater.inflate(
				R.layout.edit_text_info_window, null);
		final Dialog dialog = new AlertDialog.Builder(context).create();
		dialog.show();
		dialog.getWindow().setContentView(layout);
		dialog.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
						| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
		dialog.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
						| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		TextView tex1 = (TextView) layout.findViewById(R.id.tvEditTextInfoTitle);
		tex1.setText("昵称");
		edit = (EditText) layout.findViewById(R.id.etEditTextInfo);
		edit.setHint(contexts);
		//edit.setSelection(edit.length());
		edit.addTextChangedListener(myWatcher);
		charnum_tx = (TextView) layout.findViewById(R.id.tvEditTextInfoRemind);
		TextView btnOK = (TextView) layout.findViewById(R.id.tvEditTextInfoForward);
		btnOK.setText("确认");
		btnOK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (edit.getText().length() > 15) {
					
				} else if(edit.getText().toString()==null) {
					String[] username= {"儿子","孙子","狗","猪","许","哈哈","滚","Q","SB"};
					Random random = new Random();
					int p = random.nextInt(username.length);
					Config.UserName=username[p];
				}else {
					Config.UserName=edit.getText().toString();
				}
				dialog.dismiss();
			}
		});
		// 关闭按钮
		TextView btnClose = (TextView) layout
				.findViewById(R.id.tvEditTextInfoReturn);
		btnClose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String[] username= {"儿子","孙子","狗","猪","许","哈哈","滚","Q","SB"};
				Random random = new Random();
				int p = random.nextInt(username.length);
				Config.UserName=username[p];
				dialog.dismiss();
			}
		});
	}

	TextWatcher myWatcher = new TextWatcher() {
		@Override
		public void afterTextChanged(Editable s) {
			int len = charnum - edit.getText().length();
			if (len >= 0)
				charnum_tx.setText("您还可以输入" + len + " \\ " + charnum);
			else
				charnum_tx.setText("字数超限");
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}
	};
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值