项目结构:
核心代码:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/text_fragment_login_logo"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="32dp"
android:scaleType="centerInside"
android:textSize="28sp"
android:textColor="#fff"
android:gravity="center"
android:textStyle="bold"
android:text="停车宝(代泊员)"/>
<EditText
android:id="@+id/login_ed_shoujihao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:hint="手机号"
android:maxLength="11"
android:maxLines="1"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:numeric="integer" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/login_ed_yanzhengma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="验证码"
android:maxLength="6"
android:maxLines="1"
android:layout_marginRight="8dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"/>
<Button
android:id="@+id/login_btn_huoquyanzhengma"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="login_btn_huoquyanzhengma_onclick"
android:textColor="#fff"
android:textSize="20sp"
android:text="获取验证码" />
</LinearLayout>
<Button
android:id="@+id/login_btn_denglu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#e65400"
android:onClick="login_denglu_onclick"
android:text="登录"
android:padding="15dp"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
MainActivity
public class MainActivity extends Activity implements OnClickListener{
Thread thread=null;
private boolean tag = true;
private int i = 60;
public boolean isChange = false;
private Button btnCode=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCode = (Button) findViewById(R.id.login_btn_huoquyanzhengma);
btnCode.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.login_btn_huoquyanzhengma) {
btnCode.setText("获取验证码");
btnCode.setClickable(true);
isChange = true;
changeBtnGetCode();
}
}
private void changeBtnGetCode() {
thread = new Thread() {
@Override
public void run() {
if (tag) {
while (i > 0) {
i--;
if (MainActivity.this== null) {
break;
}
//当文本框内容改变时,结束循环。
// if (isChange && !btnCode.isClickable()) {
// isChange = false;
// break;
// }
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
btnCode.setText("获取验证码(" + i + ")");
btnCode.setClickable(false);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
tag = false;
}
i = 60;
tag = true;
if (MainActivity.this != null) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
btnCode.setText("获取验证码");
btnCode.setClickable(true);
}
});
}
};
};
thread.start();
}
}//class
没有明白下载我的例子:
http://download.csdn.net/detail/zhaihaohao1/8387081