DropEditText可拉伸EditText

DropEditText常用场景,一般在登录界面切换账号时,会出现一个弹框,里面有可供切换的账号 步骤: 这里使用到自定义组合控件,首先写一个xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25px"
    android:layout_marginRight="25px"
    android:background="@drawable/login_shape"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/user_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:hint="用户名" />

        <ImageView
            android:id="@+id/drop_down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@drawable/ic_down" />

    </RelativeLayout>

    <ImageView
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="5dp"
        android:background="#999999" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/user_psd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:hint="密码"
            android:password="true" />

    </RelativeLayout>
</LinearLayout>

2:写一个类,和此xml关联起来

public class SpinnerDemo extends LinearLayout implements ILogin{

    public SpinnerDemo(Context context) {
        super(context);
    }
    public SpinnerDemo(final Context context, AttributeSet attrs) {
        super(context, attrs);
        //把xml和class 关联起来
        View.inflate(context, R.layout.spinner,this);
        }

3:下面就可以用自定义组合控件了,在主Activity的xml中引用次控件,全类名引用

<com.dc.maritimelogistics.view.SpinnerDemo
        android:id="@+id/sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

以上就完成了自定义组合控件的引用,下面是功能部分,功能部分思路:先点击登录按钮,获得EditText的数据存入数据库,点击下拉按钮,弹出PopupWindow,PopupWindow中是一个listView,listview中适配的数据是从数据库中查询出来的先前存入数据。点击popuWindow中的数据可以给EditText赋值,即实现listview条目监听,下面是代码: 先在自定义的类中写两个方法,用来获取输入框中的值:(因为主Activity后面会用到)

//获得输入框内容
    public String getUserName(){
        return user_name.getText().toString();
    }
    public String getUserPsd(){
        return user_psd.getText().toString();
    }

主Activity中想要使用自定义类中的数据需要做如下操作: 先获取自定义类,在调其中的方法获取数值

spinnerDemo = (SpinnerDemo) findViewById(R.id.sp);

点击登录存储数据到数据库,数据库用到greenDao在Model层中进行数据库初始化,写两个方法,分别为存入和查询,这里不做详细说明,可自行百度

//点击登录,存储用户名和密码到数据库
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获得输入框数据
                String userName = spinnerDemo.getUserName();
                String userPsd = spinnerDemo.getUserPsd();
                storeDatas1.storeData(userName,userPsd);
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            }
        });

点击下拉图片弹出popuWindow

//下拉图片的监听
        drop_down.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(count%2==0){
                    drop_down.setImageResource(R.drawable.ic_up);
                    //弹出popWindow
                    ListView listView=new ListView(context);
                    //适配数据
                   // Toast.makeText(context, "list"+storeResult.searchAll()+"点击登录", Toast.LENGTH_SHORT).show();
                    //获得查询数据集合
                    users = storeResult.searchAll();
                    listView.setAdapter(new MyAdapter(context , users));
                    listView.setBackgroundColor(Color.WHITE);
                    //listview条目的监听
                    listView.setOnItemClickListener(listener);
                    //popuwindow
                    popupWindow = new PopupWindow(listView,user_name.getWidth(),160);
                    popupWindow.setOutsideTouchable(true);
                    popupWindow.setBackgroundDrawable(new ColorDrawable());

                    popupWindow.showAsDropDown(line);
                    count++;
                }else{
                    drop_down.setImageResource(R.drawable.ic_down);
                     popupWindow.dismiss();
                    count++;
                }

count为控制小箭头,因Demo需求,可忽略 以上基本实现功能 下面是点击listview,给EditText赋值,实际上就是监听listView的条目

//listview条目监听
    AdapterView.OnItemClickListener listener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //获得点击
            String username = users.get(position).getUsername();
            String psd = users.get(position).getNickname();
            //设置editText的值
            user_name.setText(username);
            user_psd.setText(psd);
            popUtil();

        }
    };

以上功能全部实现,其中数据库的存取请自行百度,这里不做详解了

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值