android 倒计时、有清除功能编辑框、popupwindow、跑马灯效果实现(基于TextView)

一、有清除功能编辑框

现在大部分应用程序在输入用户名或密码时,编辑框的右边总有一个“X”,这使得用户在不小心输入错误情况下,能快速清除,有很好的用户体验,下面讲会实现这个功能,不是我实现的,是网上的一哥们,我只是稍微作了修改。先看下布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <RelativeLayout android:id="@+id/top"
    	android:layout_width="fill_parent"
    	android:layout_alignParentTop="true"
    	android:paddingLeft="10dp"
    	android:paddingRight="10dp"
    	android:background="@drawable/top_background"
    	android:layout_height="wrap_content">
    	
        <Button android:id="@+id/btnSearch"
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center"
        	android:layout_centerVertical="true"
        	android:layout_alignParentRight="true"
        	android:textSize="12sp"
        	android:textStyle="bold"
        	android:background="@drawable/search_btn_background"
        	android:text="搜索"/>
        
        <RelativeLayout android:id="@+id/rlSearchFrameDelete"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        	android:layout_centerVertical="true"
			android:gravity="center_vertical"
            android:layout_toLeftOf="@id/btnSearch">
            
		    	<EditText android:id="@+id/etSearch"
		        	android:layout_width="fill_parent"
		        	android:layout_height="wrap_content"
					android:singleLine="true"
		        	android:background="@drawable/search_frame"
		        	android:layout_marginRight="10dp"
		        	android:paddingLeft="32dp"
		        	android:textSize="12sp"
		        	android:hint="请输入文字..."/>
		    	
		    	<ImageView android:id="@+id/ivDeleteText"
		    	    android:layout_width="wrap_content"
		    	    android:layout_height="wrap_content"
		    	    android:layout_alignParentRight="true"
		    	    android:src="@drawable/delete"
		    	    android:layout_centerInParent="true"
		    	    android:paddingRight="20dp"
		    	    android:visibility="gone"/>
            
        </RelativeLayout>
    	
    </RelativeLayout>
    
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/top"
        android:id="@+id/flow"
        android:layout_marginTop="20dp"
        >
        <com.example.deleteletter.FlowTextView 
            android:layout_width="match_parent"  
		    android:layout_height="wrap_content"  
		    android:singleLine="true"  
		    android:ellipsize="marquee"  
		    android:focusable="true"  
		    android:gravity="center_vertical"  
		    android:layout_gravity="center_vertical"  
		    android:focusableInTouchMode="true"  
		    android:marqueeRepeatLimit="marquee_forever"  
		    android:text="welcome!  欢迎光临我的博客,welcome!  欢迎光临我的博客,welcome!  欢迎光临我的博客"
            />
        
    </RelativeLayout>
    
    
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_below="@id/flow"
        >
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/showTime"
            android:gravity="center_horizontal"
            android:text="sss"
            />
        
    </RelativeLayout>
    
    
</RelativeLayout>


上面只是进行了布局,我们还要在主Activity中对EditText进行事件监听。

public class MainActivity extends Activity {
	private ImageView ivDeleteText;
	private EditText etSearch;
	private RelativeLayout top;
	private TextView showTime; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ivDeleteText = (ImageView) findViewById(R.id.ivDeleteText);
		etSearch = (EditText) findViewById(R.id.etSearch);
		top = (RelativeLayout) findViewById(R.id.top);
		showTime = (TextView) findViewById(R.id.showTime);

		ivDeleteText.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				etSearch.setText("");
			}
		});

		etSearch.addTextChangedListener(new TextWatcher() {

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

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				
				if (s.length() == 0) {
					ivDeleteText.setVisibility(View.GONE);
				} else {
					showPopWin(top); //显示popwin
					ivDeleteText.setVisibility(View.VISIBLE);
				}
			}
		});
		
		etSearch.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				etSearch.setFocusable(true);
				if(!hasFocus){
					etSearch.setFocusable(true);
				}
			}
		});
		
		//倒计时
		MycountDownTimer downTimer = new MycountDownTimer(20000, 100);
		downTimer.start();
	}
	

	/*
	 * 倒计时
	 */
	class MycountDownTimer extends CountDownTimer{

		public MycountDownTimer(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onFinish() {
			showTime.setText("倒计时完成了~");
			
		}

		@Override
		public void onTick(long millisUntilFinished) {
			showTime.setText("还有"+millisUntilFinished/1000+"秒");
		}
		
	}
	
	
	/**
	 * 展示popwin
	 */
	private void showPopWin(View v) {

		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		View pop = inflater.inflate(R.layout.popwin, null);

		ListView listView = (ListView) pop.findViewById(R.id.listview);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				MainActivity.this, android.R.layout.simple_list_item_1,
				getData());
		listView.setAdapter(adapter);
		PopupWindow window = new PopupWindow(pop, getWindowManager()
				.getDefaultDisplay().getWidth(), LayoutParams.WRAP_CONTENT,
				true);
		window.setBackgroundDrawable(new ColorDrawable(0));
		window.setOutsideTouchable(true);
		window.setFocusable(false);
		window.showAsDropDown(v, 0, 0);
	}

	/**
	 * 得到数据源
	 */

	private List<String> getData() {
		List<String> data = new ArrayList<String>();
		for (int i = 1; i <4; i++) {
			data.add("android" + i);
		}

		return data;
	}

}

因为倒计时,还有popupwinow 都写在了上面,有点乱。其中对ivDeleteText、etSearch的监听是其中的关键,以下是效果图。

这是编辑框的初始化界面:

当编辑框中有字符串时,则显示了

然后点击黑色的x 里面输入的字符讲清楚,

这里面呢,我仿照了autocompletetextview 但是不太理想,下面显示的不能获得焦点,求高手指点。

这是一哥们写的自定义autocompletetextview ,这是链接地址:http://gundumw100.iteye.com/blog/1446507


二、用TextView实现跑马灯效果

用TextView实现跑马灯效果需要在TextView中设置如下属性

<span style="white-space:pre">		</span>    android:singleLine="true"  
		    android:ellipsize="marquee"  
		    android:focusable="true"  
		    android:focusableInTouchMode="true" 
但是这样设置后,你会发现,在当TextView失去焦点后,里面的内容就不会左右浮动,所以呢,我们要重写TextView,让他一直获得焦点,代码如下

public class FlowTextView extends TextView{

	public FlowTextView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public FlowTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public FlowTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	@Override
	 protected void onFocusChanged(boolean focused, int direction,
	 Rect previouslyFocusedRect) {
	 if (focused)
	 super.onFocusChanged(focused, direction, previouslyFocusedRect);
	 }

	
	 @Override
	 public void onWindowFocusChanged(boolean focused) {
	 if (focused)
	 super.onWindowFocusChanged(focused);
	 }

	@Override
	public boolean isFocused() {
		// TODO Auto-generated method stub
		return true;
	}
}

这样就可以实现跑马灯效果了。

三、倒计时实现

现在很多手机应用在第一次打开时候,都会让用户等待几秒,来显示他们的广告,当然了倒计时的实现可以用Handler实现,但是android给我们提供了一个专门类CountDownTimer 我们只要继承此类,就可以简单实现倒计时。

/*
	 * 倒计时
	 */
	class MycountDownTimer extends CountDownTimer{

		public MycountDownTimer(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onFinish() {
			showTime.setText("倒计时完成了~");
			
		}

		@Override
		public void onTick(long millisUntilFinished) {
			showTime.setText("还有"+millisUntilFinished/1000+"秒");
		}
		
	}

其中millisInFuture代表倒计时总的时长,countDownInterval代表时间间隔,onFinish方法是倒计时完成后需要执行的代码。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值