MainActivity.java
package com.example.lab2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText et1; //输入用户名的EditText
EditText et2; //输入密码的EditText
ImageButton ib; //声明ImageButton
Button btn; //重置按钮
Context context;
LinearLayout mLinearLayout; //这个LineatLayout用于放置添加的组件
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init(); //初始化
//单击imagebutton
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(et1.getText().toString().equals("android") && et2.getText().toString().equals("2015"))
{
ib.setImageResource(R.drawable.state1); //设置改变原来的图片
et1.setVisibility(View.GONE);
et2.setVisibility(View.GONE);
}
else //输入账号或者密码错误的时候
{
et2.setText("");
et2.setHint("账号或密码错误");
}
}
};
ib.setOnClickListener(listener);
//长按imagebutton
OnLongClickListener longlistener = new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(context, "动态添加了TextView了哦", Toast.LENGTH_SHORT);
tv = new TextView(context);
tv.setText("这里是动态添加的TextView");
mLinearLayout.addView(tv); //添加组件
return false;
}
};
ib.setOnLongClickListener(longlistener);
//重置
OnClickListener listener2 = new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ib.setImageResource(R.drawable.state2);
et1.setHint("请输入用户名");
et2.setHint("请输入密码");
et1.setText("");
et2.setText("");
et2.requestFocus(); //将光标定位EditText
et1.setVisibility(View.VISIBLE);
et2.setVisibility(View.VISIBLE);
mLinearLayout.removeAllViews(); //点击重置按钮的时候,也将原来添加的组件一起清除
}
};
btn.setOnClickListener(listener2);
}
//初始化函数
public void init()
{
et1 = (EditText)findViewById(R.id.et_username);
et2 = (EditText)findViewById(R.id.et_passward);
ib = (ImageButton)findViewById(R.id.ib);
btn = (Button)findViewById(R.id.btn);
context = MainActivity.this;
mLinearLayout = (LinearLayout)findViewById(R.id.linearLayout);
}
}
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"
>
<EditText
android:id="@+id/et_username"
android:hint="请输入用户名"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/et_passward"
android:hint="请输入密码"
android:inputType="textPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/ib"
android:src="@drawable/state2"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:id="@+id/btn"
android:text="重置"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>