1. 目标页面
结合前面练习用到的控件,实现一个简单的登陆界面,用户输入相关信息以后,单击登陆按钮,跳转到下一个页面,并在第二个页面显示用户的信息,页面如下:
在这里,要设置页面为横屏显示,以及需要两个布局页面两个分开的java逻辑,具体如下。
2.页面布局
a.第一个页面的xml布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg1"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<EditText
android:id="@+id/name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginLeft="100dp"
android:layout_marginTop="45dp"
android:hint="请输入您的用户名"
android:textColor="#FFFFFF">
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入您的密码"
android:password="true"
android:layout_marginLeft="100dp"
android:layout_marginTop="10dp"
android:textColor="#FFFFFF"/>
<View
android:layout_width="220dp"
android:layout_height="2dp"
android:layout_marginLeft="90dp"
android:background="#666666"/>
<RadioGroup
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:id="@+id/radio"
>
<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textColor="#FFFFFF"
/>
<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="女"
android:textColor="#FFFFFF" />
</RadioGroup>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择您的英雄:"
android:textColor="#FFFFFF"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/people"
/>
</LinearLayout>
<Button
android:id="@+id/login"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"
android:background="@android:color/darker_gray"
android:layout_marginLeft="250dp"/>
</LinearLayout>
*在上面的列表选择框Spinner控件中的android:entries="@array/people"这个属性,要新建一个数组资源文件(在res/values文件夹下新建一个array的xml文件),资源文件如下:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<string-array name="people">
<item android:color="#FFFFFF" >射手</item>
<item android:color="#FFFFFF" >法师</item>
<item android:color="#FFFFFF" >战士</item>
<item android:color="#FFFFFF" >辅助</item>
<item android:color="#FFFFFF" >刺客</item>
</string-array>
</resources>
<?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"
android:background="@drawable/bg2" >
<EditText
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:layout_marginLeft="20dp"
android:ems="10"
android:text="这里显示您的内容"
android:textColor="#FFFFFF"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</LinearLayout>
3.事件响应
a.首先在主类中将第一个页面的值获得,并传入第二个页面中。获得第一个界面的值,需要为响应的控件添加监听器。具体作法如下:
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity
{
TextView name=null;
TextView password=null;
RadioGroup radio=null;
RadioButton radioButton=null;
Spinner hero=null;
String selectHero=null;
Button login=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//第一个界面---获取控件
name=(TextView) findViewById(R.id.name);
password=(TextView) findViewById(R.id.password);
radio=(RadioGroup) findViewById(R.id.radio);
hero=(Spinner) findViewById(R.id.spinner);
login=(Button) findViewById(R.id.login);
//第一个界面---添加事件监听
radio.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1)
{
//arg1是单选按钮组中选中的单选按钮
radioButton=(RadioButton) findViewById(arg1);
}
});
hero.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
selectHero=arg0.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//第一个界面---按钮的事件监听,在此跳转
login.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
//使用Intent跳转到第二个页面,并进行传值
Intent i=new Intent(MainActivity.this,NextActivity.class);
i.putExtra("name", name.getText().toString());
i.putExtra("pw", password.getText().toString());
i.putExtra("sex", radioButton.getText().toString());
i.putExtra("hero", selectHero);
startActivity(i);
}
});
}
@Override
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;
}
}
b.跳转的第二个如下:
package com.example.demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class NextActivity extends Activity
{
TextView show=null;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
show=(TextView) findViewById(R.id.show);
Intent it=getIntent();
String name=it.getStringExtra("name");
String password=it.getStringExtra("pw");
String sex=it.getStringExtra("sex");
String selectHero=it.getStringExtra("hero");
show.setText(name+"欢迎进入王者荣耀!\n"+"您的登陆密码为:"+password+
"\n选择的英雄为:"+selectHero);
}
}
c.新建的
NextActivity.java必须要继承Activity,并且要在清单文件中注册,主要如下:
<activity
android:name=".NextActivity"></activity>
4.运行效果
3.事件响应