zoukankan      html  css  js  c++  java
  • eatwhatApp开发实战(十三)

      这次内容,我们就项目中添加商店名称的EditText进行修改,让添加按钮随着edittext的内容而改变。

      上代码,首先是xml文件上对两个控件的修改:

    <RelativeLayout 
            android:id="@+id/et_relative"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText 
    		    android:id="@+id/addshop_et"
    		    android:layout_width="match_parent"
    		    android:layout_height="wrap_content"
    		    android:textSize="18sp"
    		    android:hint="店名"/>
    	<TextView 
    		    android:id="@+id/add_shop_text"
    		    android:layout_width="wrap_content"
    		    android:layout_height="wrap_content"
    		    android:layout_alignParentRight="true"
    		    android:focusable="true"
    		    android:onClick="addShop"
    		    android:textSize="18sp"
    		    android:layout_marginRight="5dip"
    		    android:layout_marginTop="5dip"
    		    android:text="@string/add_shop_btn_text"
    		    android:visibility="gone"/>
        </RelativeLayout>
    

      接下来定义,并初始化对应控件:

    private TextView add_shop_text;
    		
    // 初始化控件 add_text
    add_shop_text = (TextView) findViewById(R.id.add_shop_text);
    // 初始化控件addshop_EditText
    addshop_et = (EditText) findViewById(R.id.addshop_et);
    //edittext设置监听
    addshop_et.addTextChangedListener(new TextChanged());
    

      设置edittext的修改文本内容监听addTextChangedListener();

        class TextChanged implements TextWatcher{
    
    		@Override
    		public void beforeTextChanged(CharSequence s, int start, int count,
    				int after) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void onTextChanged(CharSequence s, int start, int before,
    				int count) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void afterTextChanged(Editable s) {
    			
    
    		}
    	}
    

      在afterTextChanged(Editable s)方法中写对应逻辑:

      if(s != null && !"".equals(s.toString())){
        add_shop_text.setVisibility(View.VISIBLE);
      }else {
        add_shop_text.setVisibility(View.INVISIBLE);
      }
    

      这样就完成了这个功能,在文本框有内容输入时添加功能就会出现。

  • 相关阅读:
    Java内存模型(JMM)
    线程安全问题的本质详解: 原子性、有序性、可见性
    Quartz实现分布式可动态配置的定时任务
    Java引用详解-StrongReference SoftReference WeakReference PhantomReference
    流行的报表生成工具-JXLS
    Java线程监控及中断
    IntelliJ IDEA 内存优化最佳实践
    Dapeng框架-开源高性能分布式微服务框架
    Scala实现Try with resources自动关闭IO
    Jvm启动,关闭及对应钩子
  • 原文地址:https://www.cnblogs.com/superdo/p/5223717.html
Copyright © 2011-2022 走看看