1.如何给ImageButton设置背景图片->用 src=“@drawable/****”
2.如何在xml隐藏ImageButton -> visibility= "gone"
3.能不能对文本编辑框进行监听? ->能,用addTextChangedListener(new TextWatcher{})
4.如何在activity里实现imagebutton的隐藏与显示
setVisibility(View.VISIBLE);显示
setVisibility(View.GONE);隐藏
5.如何显示和隐藏密码文本?如何分别监听按钮的按下和弹开?->在demo中注释给出
<RelativeLayout 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:background="#f2f2f2" tools:context="com.example.administrator.icephonefhhxml.ForgottenPassword"> <View android:layout_height="2dp" android:layout_width="fill_parent" android:background="#dddddd" android:id="@+id/v0" /> <View android:layout_height="0.5px" android:layout_width="fill_parent" android:background="#ebebeb" android:id="@+id/v1" android:layout_below="@+id/v0" android:layout_marginTop="12dp" /> <LinearLayout android:id="@+id/firstBlock" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignTop="@+id/v1" android:background="#FFFFFF" android:orientation="vertical" > <LinearLayout android:id="@+id/Blockline1" android:layout_width="fill_parent" android:layout_height="48dp" android:layout_alignTop="@+id/v1" android:background="#FFFFFF" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv_phone" android:layout_marginLeft="21dp" android:layout_marginTop="12dp" android:src="@drawable/my_icon_mobile"/> <EditText android:layout_width="150dp" android:layout_height="wrap_content" android:layout_marginLeft="17dp" android:layout_marginTop="15dp" android:id="@+id/edtTxt_phone_number" android:hint="请输入手机号" android:background="@null" android:phoneNumber="true"//设置文本类型是电话号码 android:maxLength="11" //设置最大长度 android:textColor="#404548" android:textCursorDrawable="@drawable/cursor_style"//设置光标风格(第一篇文章讲过这个方法) android:textSize="16sp"/> <Button android:id="@+id/btn_getting_code" android:layout_width="100dp" android:layout_height="35dp" android:layout_marginLeft="13dp" android:layout_marginTop="12dp" android:background="@drawable/code_style"//设置按钮风格,这里可以用普通图片替代 android:textColor="#404548" android:textSize="14sp" android:text="获取验证码" /> </LinearLayout> <View android:layout_height="0.5px" android:layout_width="fill_parent" android:background="#ebebeb" android:id="@+id/v3" android:layout_marginTop="17dp" /> <LinearLayout android:id="@+id/Blockline2" android:layout_width="fill_parent" android:layout_height="48dp" android:layout_alignTop="@+id/v3" android:background="#FFFFFF" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv_code_lock" android:layout_marginLeft="19dp" android:layout_marginTop="12dp" android:src="@drawable/my_icon_lock"/> <EditText android:layout_width="150dp" android:layout_height="wrap_content" android:layout_marginLeft="13dp" android:layout_marginTop="16.5dp" android:id="@+id/edtTxt_auth_code" android:hint="验证码" android:background="@null" android:maxLength="11" android:textColor="#404548" android:textCursorDrawable="@drawable/cursor_style" android:textSize="16sp"/> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imgbtn_delete" android:layout_marginLeft="80dp" android:layout_marginTop="16.5dp" android:src="@drawable/change_city_roundadd" android:visibility="gone" android:background="@null"/> </LinearLayout> <View android:layout_height="0.5px" android:layout_width="fill_parent" android:background="#ebebeb" android:id="@+id/v4" android:layout_marginTop="17dp" /> <LinearLayout android:id="@+id/Blockline3" android:layout_width="fill_parent" android:layout_height="48dp" android:layout_alignTop="@+id/v4" android:background="#FFFFFF" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv_code_resetting_lock" android:layout_marginLeft="19dp" android:layout_marginTop="12dp" android:src="@drawable/my_icon_lock"/> <EditText android:layout_width="150dp" android:layout_height="wrap_content" android:layout_marginLeft="13dp" android:layout_marginTop="16.5dp" android:id="@+id/edtTxt_code_resetting" android:hint="重设登录密码" android:background="@null" android:maxLength="11" android:textColor="#adadad" android:textCursorDrawable="@drawable/cursor_style" android:textSize="16sp" android:password="true"/> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imgbtn_look" android:layout_marginTop="18dp" android:layout_marginLeft="78dp" android:src="@drawable/btn_look_style" android:background="@null"/> </LinearLayout> <View android:layout_height="0.5px" android:layout_width="fill_parent" android:background="#ebebeb" android:id="@+id/v5" android:layout_marginTop="17dp" /> </LinearLayout> <Button android:id="@+id/btn_complete" android:layout_width="fill_parent" android:layout_height="46dp" android:layout_below="@+id/firstBlock" android:layout_marginLeft="12dp" android:background="@drawable/button_style" android:layout_marginRight="12dp" android:layout_marginTop="30dp" android:text="完成" android:textColor="#FFFFFF" android:textSize="18sp" android:focusable="true"/> </RelativeLayout>
Activity代码
package com.example.administrator.icephonefhhxml; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.text.method.HideReturnsTransformationMethod; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.TextView; import static android.text.method.PasswordTransformationMethod.*; public class ForgottenPassword extends Activity { TextView tittle; Button back; ImageButton imgBtn_show_code; EditText edtTxt_password; EditText edtTxt_code; ImageButton imgBtn_delete_code; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_forgotten_password); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tittle_bar);//设置标题栏,之前有讲过 tittle = (TextView)findViewById(R.id.tv_tittle); tittle.setText("忘记密码"); back = (Button)findViewById(R.id.btn_back); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); //各种获取监听 edtTxt_password = (EditText)findViewById(R.id.edtTxt_code_resetting) ; imgBtn_show_code = (ImageButton)findViewById(R.id.imgbtn_look) ; edtTxt_code =(EditText)findViewById(R.id.edtTxt_auth_code); imgBtn_delete_code = (ImageButton)findViewById(R.id.imgbtn_delete); ButtonListener b = new ButtonListener(); imgBtn_show_code.setOnTouchListener(b); imgBtn_delete_code.setOnClickListener(new View.OnClickListener() { //点击按钮删除文本框中的内容 @Override public void onClick(View v) { edtTxt_code.setText(""); } }); edtTxt_code.addTextChangedListener(new TextWatcher() { //监听文本框的输入 @Override public void onTextChanged(CharSequence s, int start, int before, int count) {//在正要输入时 // TODO Auto-generated method stub if(edtTxt_code.length() != 0)//如果文本框中有内容 { imgBtn_delete_code.setVisibility(View.VISIBLE);//删除按钮显现出来 }else { imgBtn_delete_code.setVisibility(View.GONE);//否则按钮隐藏 } } @Override public void beforeTextChanged(CharSequence s, int start, int count,//在输入前 int after) { // TODO Auto-generated method stub if(edtTxt_code.length() != 0) { imgBtn_delete_code.setVisibility(View.VISIBLE); }else { imgBtn_delete_code.setVisibility(View.GONE); } } @Override public void afterTextChanged(Editable s) {//在输入后 // TODO Auto-generated method stub if(edtTxt_code.length() != 0) { imgBtn_delete_code.setVisibility(View.VISIBLE); }else { imgBtn_delete_code.setVisibility(View.GONE); } } }); } class ButtonListener implements View.OnTouchListener//用来控制显示按钮的监听(就是那个眼睛) { @Override public boolean onTouch(View v, MotionEvent event) { if(v.getId() == R.id.imgbtn_look)//如果点击了这个按钮 { if(event.getAction() == MotionEvent.ACTION_UP)//按钮弹起 { edtTxt_password.setTransformationMethod(getInstance());//隐藏 edtTxt_password.setSelection(edtTxt_password.length());//这句是设置光标位置到最后 } if(event.getAction() == MotionEvent.ACTION_DOWN)//按钮按下 { edtTxt_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示隐藏的内容 edtTxt_password.setSelection(edtTxt_password.length());//设置光标位置到最后 } } return false; } } }
大概内容就是这样,不过由于标题栏的原因本文中的代码完全复制粘贴过去并不能运行,需要和之前设置标题栏的文章结合一下~