仿安卓QQ界面布局活动

运行效果如下,


MainActivity.java代码如下

package com.example.qq;


import com.example.qqq.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
private EditText txtlogin, txtpwd;
private ImageView roundImageView, eliminateimage, pwdimage;
private Button login;
private TextView fwtk, wufadenglu;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtpwd = (EditText) findViewById(R.id.pwd);


txtlogin = (EditText) findViewById(R.id.txtlogin);// 找到文本框
txtlogin.setInputType(EditorInfo.TYPE_CLASS_PHONE); // 设置输入法类型


login = (Button) findViewById(R.id.login);
eliminateimage = (ImageView) findViewById(R.id.eliminateimage);
pwdimage = (ImageView) findViewById(R.id.pwdimage);
roundImageView = (ImageView) findViewById(R.id.roundImageView);


login.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
String txtlogin1 = txtlogin.getText().toString();
String pwd1 = txtpwd.getText().toString();
if (txtlogin1.equals("")) {
Toast.makeText(MainActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
} else if (pwd1.equals("")) {
Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
}
}


});
// 给账号文本框添加文本框改变监听事件,下面就是方法体
txtlogin.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) {
}
public void afterTextChanged(Editable s) {
// afterTextChanged方法就是一直监听文本框,s是文本框的内容,是方法自带的
if (s.length() != 0) {
// 如果内容不是空,就设置图片为清除的那个图片
eliminateimage.setImageResource(R.drawable.common_input_box_clear);
// 给清除的图片添加监听事件
eliminateimage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// 设置账号和密码框为空
txtlogin.setText(null);
txtpwd.setText(null); }
});
// 下面是输入的账号为我的QQ号的时候,显示我的头像,不是的话显示本来的头像
String str3 = "1406423298";
if (s.toString().equals(str3)) {
roundImageView.setImageResource(R.drawable.asas);
} else {
roundImageView.setImageResource(R.drawable.gco);
}
} else {
// 是空的话就设置 图片为空
eliminateimage.setImageURI(null);
}
}
});
// 密码框和上面的一样的效果
txtpwd.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) {
}


public void afterTextChanged(Editable s) {
if (s.length() != 0) {
pwdimage.setImageResource(R.drawable.common_input_box_clear);
pwdimage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
txtpwd.setText(null);
}
});
} else {
pwdimage.setImageURI(null);
;
}
}
});
fwtk = (TextView) findViewById(R.id.fuwu);
fwtk.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Fuwutiankuan.class);
startActivity(intent);
}
});
wufadenglu = (TextView) findViewById(R.id.onlogin);
wufadenglu.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// startActivity(new
// Intent(MainActivity.this,SelectPicPopupWindow.class));
}
});
}


// onKeyDown方法,该方法是接口KeyEvent.Callback中的抽象方法
// 参数keyCode,该参数指的是被按下的键的键码
/*返回值,该方法的返回值为一个boolean类型的变量,当返回true时,
表示已经完整地处理了这个事件,并不希望其他的回调方法再次进行处理,而当返回false时,
表示并没有完全处理完该事件,更希望其他回调方法继续对其进行处理,例如Activity中的回调方法*/
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}


public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

布局代码如下

<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="@color/huise"
    tools:context="com.example.qq.MainActivity" >


    <com.example.qq.XCRoundImageView
        android:id="@+id/roundImageView"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:src="@drawable/gco" />


    <EditText
        android:id="@+id/txtlogin"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_below="@+id/roundImageView"
        android:layout_marginTop="25dp"
        android:background="#ffffff"
        android:ems="10"
        android:hint="    QQ号/手机号/邮箱"
        android:inputType="text"
        android:maxLength="13"
        android:singleLine="true"
        android:textColor="#000000"
        android:textCursorDrawable="@drawable/color_cursor"
        android:textSize="16dp" />


    <ImageView
        android:id="@+id/eliminateimage"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_below="@+id/roundImageView"
        android:layout_marginLeft="250dp"
        android:layout_marginTop="45dp"
        android:src="@drawable/dsfsgs" />


    <EditText
        android:id="@+id/pwd"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_below="@+id/txtlogin"
        android:layout_marginTop="1dp"
        android:background="#ffffff"
        android:ems="10"
        android:hint="  密码"
        android:inputType="textPassword"
        android:maxLength="16"
        android:singleLine="true"
        android:textColor="#000000"
        android:textCursorDrawable="@drawable/color_cursor"
        android:textSize="16dp" />


    <ImageView
        android:id="@+id/pwdimage"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_below="@+id/eliminateimage"
        android:layout_centerVertical="true"
        android:layout_marginLeft="250dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/dsfsgs" />


    <TextView
        android:id="@+id/onlogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:text="无法登陆?"
        android:textColor="@color/skyblue"
        android:textSize="14dp" />


    <TextView
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:text="新用户注册"
        android:textColor="@color/skyblue"
        android:textSize="14dp" />


    <Button
        android:id="@+id/login"
        android:layout_width="276dp"
        android:layout_height="40dp"
        android:layout_below="@+id/pwd"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:background="@drawable/shape"
        android:text="登录"
        android:textSize="16dp" />


    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="20dp"
        android:layout_alignLeft="@+id/onlogin"
        android:layout_below="@+id/pwd"
        android:button="@null"
        android:background="@drawable/checkbox_style"
 />


    <TextView
        android:id="@+id/yuedy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/checkBox1"
        android:layout_toRightOf="@+id/checkBox1"
        android:text="我已阅读并同意"
        android:textColor="@color/black"
        android:textSize="14dp" />


    <TextView
        android:id="@+id/fuwu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/yuedy"
        android:layout_alignBottom="@+id/yuedy"
        android:layout_toRightOf="@+id/yuedy"
        android:text=" 服务条款"
        android:textColor="@color/skyblue"
        android:textSize="14dp" />


</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/qq_32448349/article/details/52714210