一、效果图如下
在开发的时候,有时需要可控的显示和隐藏输入框,下面是自己写的demo,贴出来共享
activity_update_password.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/titlebar_update_password" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="2dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/Current_Password" />
<TextView
android:id="@+id/activity_update_show01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:clickable="true"
android:onClick="showPassword"
android:padding="5dp"
android:text="@string/Show_Password"
android:textColor="@drawable/read_more_btn"
android:textSize="10sp" />
</RelativeLayout>
<EditText
android:id="@+id/activity_update_currentPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:drawable/edit_text"
android:hint="@string/Current_Password"
android:singleLine="true"
android:textColor="#000000"
android:inputType="textPassword"
android:textCursorDrawable="@null" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/New_Password" />
<TextView
android:id="@+id/activity_update_show02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:clickable="true"
android:onClick="showPassword"
android:padding="5dp"
android:text="@string/Show_Password"
android:textColor="@drawable/read_more_btn"
android:textSize="10sp" />
</RelativeLayout>
<EditText
android:id="@+id/activity_update_newPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:drawable/edit_text"
android:hint="@string/New_Password"
android:singleLine="true"
android:textColor="#000000"
android:inputType="textPassword"
android:textCursorDrawable="@null" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/confirm_password" />
<TextView
android:id="@+id/activity_update_show03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:clickable="true"
android:onClick="showPassword"
android:padding="5dp"
android:text="@string/Show_Password"
android:textColor="@drawable/read_more_btn"
android:textSize="10sp" />
</RelativeLayout>
<EditText
android:id="@+id/activity_update_confirmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:drawable/edit_text"
android:hint="@string/confirm_password"
android:singleLine="true"
android:textColor="#000000"
<span style="color:#ff6666;"> android:inputType="textPassword"</span>
android:textCursorDrawable="@null" />
<Button
android:id="@+id/activity_update_updatePassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/theme_btn"
android:text="@string/Update_Password" />
</LinearLayout>
</LinearLayout>
Update按钮点击效果
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/theme_blue_normal" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="@drawable/theme_blue_normal" android:state_enabled="false"/>
<item android:drawable="@drawable/theme_blue_press" android:state_pressed="true"/>
<item android:drawable="@drawable/theme_blue_normal" android:state_focused="true"/>
</selector>
需要提供2张图片背景即可,即点击前后的背景图片
文字点击效果<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#f00"/>
<item android:state_focused="true" android:color="@color/activity_search_title_bg"/>
<item android:color="@color/activity_search_title_bg"/>
</selector>
需要2种颜色,即点击前后的文字颜色
主类
package com.chinabuye.android.activity;
import http.HttpRestClient;
import http.JsonHttpResponseHandler;
import org.json.JSONException;
import org.json.JSONObject;
import tool.NewTokenCallBack;
import tool.TokenTools;
import tool.UserInfo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.chinabuye.android.R;
public class UpdatePasswordActivity extends Activity implements OnClickListener {
private EditText currentPassword;
private EditText newPassword;
private EditText confirmPassword;
private ImageView back;
private Button updateBtn;
private AlertDialog dialog;
<span style="color:#ff6666;">private boolean mbDisplayFlg01 = false; // 用于动态显示和隐藏密码
private boolean mbDisplayFlg02 = false;
private boolean mbDisplayFlg03 = false;</span>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
manageActivity();
setContentView(R.layout.activity_update_password);
initView();
}
private void initView() {
currentPassword = (EditText) this.findViewById(R.id.activity_update_currentPassword);
newPassword = (EditText) this.findViewById(R.id.activity_update_newPassword);
confirmPassword = (EditText) this.findViewById(R.id.activity_update_confirmPassword);
updateBtn = (Button) this.findViewById(R.id.activity_update_updatePassword);
updateBtn.setOnClickListener(this);
back = (ImageView) this.findViewById(R.id.activity_update_back);
back.setOnClickListener(this);
createDialog();
}
private void manageActivity() {
ActivityInstanceManager.getActivityInstanceManager().addActivity(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.activity_update_updatePassword:
String currentPass = currentPassword.getText().toString().trim();
String newPass = newPassword.getText().toString().trim();
String confirmPass = confirmPassword.getText().toString().trim();
if ("".equals(currentPass) || currentPass == null) {
Toast.makeText(this, R.string.Current_password_cannot_be_empty, Toast.LENGTH_LONG).show();
break;
}
if ("".equals(newPass) || newPass == null) {
Toast.makeText(this, R.string.New_password_connot_be_empty, Toast.LENGTH_LONG).show();
break;
}
if ("".equals(confirmPass) || confirmPass == null) {
Toast.makeText(this, R.string.confirm_password_connot_be_empty, Toast.LENGTH_LONG).show();
break;
}
if (!confirmPass.equals(newPass)) {
Toast.makeText(this, R.string.The_password_donnot_match, Toast.LENGTH_LONG).show();
break;
}
//判断输入的格式是否是邮箱格式
if (currentPass.equals(newPass)) {
Toast.makeText(this, R.string.The_same_password_enter_again, Toast.LENGTH_LONG).show();
break;
}
updatePasswordMethod(currentPass, newPass);
break;
case R.id.activity_update_back:
finish();
break;
default:
break;
}
}
public void showPassword(View view) {
<span style="color:#ff6666;">switch (view.getId()) {
case R.id.activity_update_show01:
if (!mbDisplayFlg01) {
// display password text, for example "123456",从隐藏到显示
currentPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
// hide password, display ".",从显示到隐藏
currentPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
mbDisplayFlg01 = !mbDisplayFlg01;
currentPassword.postInvalidate();
break;
case R.id.activity_update_show02:
if (!mbDisplayFlg02) {
// display password text, for example "123456",从隐藏到显示
newPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
// hide password, display ".",从显示到隐藏
newPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
mbDisplayFlg02 = !mbDisplayFlg02;
newPassword.postInvalidate();
break;
case R.id.activity_update_show03:
if (!mbDisplayFlg03) {
// display password text, for example "123456",从隐藏到显示
confirmPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
// hide password, display ".",从显示到隐藏
confirmPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
mbDisplayFlg03 = !mbDisplayFlg03;
confirmPassword.postInvalidate();
break;
default:
break;
}</span>
}
private void updatePasswordMethod(final String currentPass, final String newPass) {
TokenTools.requestNewToken(this, new NewTokenCallBack() {
@Override
public void getNewTokenSuccess(String newAccessToken) {
if (newAccessToken != null) {
String customerid = UserInfo.getUserInfoInstance().getUserid();
if (customerid == null || "".equals(customerid)) {
Toast.makeText(UpdatePasswordActivity.this, R.string.There_are_some_problems_with_customerid, Toast.LENGTH_LONG).show();
} else {
HttpRestClient.updatePassword(UpdatePasswordActivity.this, customerid, currentPass, newPass, newAccessToken, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject response) {
super.onSuccess(response);
try {
String result = response.getString("result");
if ("true".equals(result)) {
String desc = response.getString("desc");
handler.sendEmptyMessage(1);
} else {
Toast.makeText(UpdatePasswordActivity.this, UpdatePasswordActivity.this.getResources().getString(R.string.Some_problems_have_arisen), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
}
});
}
private Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
if (dialog != null)
dialog.show();
break;
default:
break;
}
};
};
private void createDialog() {
dialog = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher).setCancelable(false).setTitle("Exit Dialog").setMessage(R.string.You_have_successfully_changed_the_password)
.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 将存储在本地的密码设置为空
SharedPreferences sp = UpdatePasswordActivity.this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
Editor editor = sp.edit();
editor.putBoolean("isLogout", true);
editor.putString("PASSWORD", null);
editor.commit();
Intent intent = new Intent(UpdatePasswordActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}).create();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (dialog.isShowing()) {
return true;
} else {
Intent intent = new Intent(UpdatePasswordActivity.this, HelpAboutActivity.class);
intent.setAction("update_to_helpabout");
startActivity(intent);
finish();
}
}
return super.onKeyDown(keyCode, event);
}
}
主类是我从项目中抠出来的,关键部分用红色标记。