1、理解Android布局管理器及其属性,掌握线性布局用户界面(UI)的设计方法。
2、掌握在XML文件和Java代码中访问字符串、数组、颜色和尺寸等资源的方法。
3、掌握在Java代码中获取UI控件对象及事件处理的方法。
设计并实现一个电子邮件APP的模拟程序,完成用户登录和发送邮件的功能,具体要求如下:
1. 定义字符串数组资源,保存可登录的用户。
2. 采用线性布局定义XML布局资源,使用EditText、Button和TextView控件定义用户界面。
3. 在Activity类中,获取控件对象,采用匿名类为相关控件添加事件监听器,完成事件处理。
4. 使用Toast对象显示相关运行信息。
acticty_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/side"
android:orientation="vertical"
tools:context="cn.yimispace.email.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="用户名"/>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录邮箱"/>
LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/split_height"
android:background="@color/colorPrimary"/>
<EditText
android:id="@+id/to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="收件人"/>
<EditText
android:id="@+id/from"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:enabled="false"
android:hint="发件人"/>
<EditText
android:id="@+id/theme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="主题"/>
<EditText
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="@string/message"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="发送"
android:layout_gravity="right"/>
LinearLayout>
MainActivity.java
package cn.yimispace.email;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
private EditText username,to,from,theme,message;
private Button login,send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.username);
to = (EditText)findViewById(R.id.to);
from = (EditText)findViewById(R.id.from);
theme = (EditText)findViewById(R.id.theme);
message = (EditText)findViewById(R.id.message);
login = (Button)findViewById(R.id.login);
send = (Button)findViewById(R.id.send);
login.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String[] users = MainActivity.this.getResources().getStringArray(R.array.users);
boolean hasUser = false;
for (String user:users) {
if (user.equals(username.getText().toString())) {
from.setText(user
+ "@" + MainActivity.this.getResources().getString(R.string.email));
message.append("\n\t\t\t签名:" + user);
send.setEnabled(true);
hasUser = true;
break;
}
}
if (!hasUser) {
Toast.makeText(MainActivity.this, username.getText() + "用户不存在!", Toast.LENGTH_LONG).show();
}
}
});
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "正在向" + to.getText()
+ "发送邮件...", Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "邮件发送成功!",
Toast.LENGTH_LONG).show();
}
});
}
}