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);
  }

  這樣就完成了這個功能,在文本框有內容輸入時添加功能就會出現。

相關文章
相關標籤/搜索