实例11:仿QQ客户端登陆界面

1、创建login.xml文件

<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/tableLayout1"
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:gravity="center_vertical"
	android:stretchColumns="0,3"
	>
	<!-- 第一行 -->
	<TableRow android:id="@+id/tableRow1" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
		<TextView/>
		<TextView android:text="帐    号:" 
			android:id="@+id/textView1" 
			android:layout_width="wrap_content"
			android:textSize="24px" 
			android:layout_height="wrap_content"
			/>
		<EditText android:id="@+id/editText1" 
			android:textSize="24px" 
			android:layout_width="wrap_content" 
			android:layout_height="wrap_content" android:minWidth="200px"/>
		<TextView />
	</TableRow>
	<!-- 第二行 -->	
	<TableRow android:id="@+id/tableRow2" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
		<TextView/>
		<TextView android:text="密    码:" 
			android:id="@+id/textView2" 
			android:textSize="24px" 
			android:layout_width="wrap_content" 
			android:layout_height="wrap_content"/>
		<EditText android:layout_height="wrap_content" 
			android:layout_width="wrap_content" 
			android:textSize="24px" 
			android:id="@+id/editText2" 
			android:inputType="textPassword"/>
		<TextView />
	</TableRow>
	<!-- 第3行 -->
	<TableRow android:id="@+id/tableRow3" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
		<TextView/>
		<Button android:text="登录" 
			android:id="@+id/login" 
			android:layout_width="wrap_content" 
			android:layout_height="wrap_content"/>
		<Button android:text="退出" 
			android:id="@+id/exit" 
			android:layout_width="wrap_content" 
			android:layout_height="wrap_content"/>
		<TextView />
	</TableRow>
</TableLayout>

2、创建Data类

public final class Data {
	//用户信息
	public static String[][] USER = {
		{"1001","111","Jaer"},
		{"1002","111","Soft"},
		{"1003","111","Lol"},
	};
}

3、创建一个继承android.app.Activity的LoginAcitivity,java

public class LoginActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);//设置该Activity
		Button button = (Button)findViewById(R.id.login);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String number = ((EditText)findViewById(R.id.editText1)).getText().toString();
				String pwd = ((EditText)findViewById(R.id.editText2)).getText().toString();
				boolean flag = false;//用于记录登录是否成功的标记变量
				String nickname = "";//保存昵称
				//通过遍历数据的形式判断输入的账号和密码是否正确
				for (int i = 0; i < Data.USER.length; i++) {
					if(number.equals(Data.USER[i][0])){//判断账号是否正确
						if(pwd.equals(Data.USER[i][1])){//判断密码是否正确 
							nickname = Data.USER[i][2];//获得昵称
							flag = true;//将标志变量设置为true
							break;//跳出for循环
						}
					}
				}
				if(flag){
					//创建要显示 Activity对应的Intent对象
					Intent intent = new Intent(LoginActivity.this, MainActivity.class);
					Bundle bundle =new Bundle();//创建一个Bundle的对象的bundle
					bundle.putString("nickname", nickname);//保存昵称
					intent.putExtras(bundle);//将数据包添加到Intent对象中
					startActivity(intent);//开启一个新的Activity
				}else{
					Toast.makeText(LoginActivity.this, "您输入的账号或密码错误!", Toast.LENGTH_SHORT).show();
				}
			}
		});
		Button exit = (Button)findViewById(R.id.exit);
		exit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();//关闭当前Activity
			}
		});
	}
}	


4、activity_main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/nickname"
                android:layout_width="wrap_content"
                android:layout_weight="9"
                android:textSize="24px"
                android:padding="20px"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <Button
                android:id="@+id/m_exit"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="退出登录" />
            
        </LinearLayout>

        <ListView
            android:id="@+id/listView1"
            android:entries="@array/option"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>

5、在res\value目录中创建一个定义数组资源的xml文件arrays.xml,并在该文件中添加名称为option的字符串数组

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string-array name="option">
	    <item >在线好友</item>
	    <item >我的好友</item>
	    <item >陌生人</item>
	    <item >黑名单</item>
	</string-array>    
</resources>

6、在MainActivity的onCreate()方法中

Intent intent = getIntent();//获取Intent对象
        Bundle bundle = intent.getExtras();//获取传递的数据包
        String nickname = bundle.getString("nickname");//获取传递过来的昵称
        TextView tv = (TextView)findViewById(R.id.nickname);//获取用于显示当前的登录用户的TextView组件
        tv.setText("当前登录:"+nickname);//显示当前登录用户的昵称
        Button button = (Button)findViewById(R.id.m_exit);//获取“退出登录”按钮
        button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();//关闭当前Activity
			}
		});

7、AndroidManifest.xml文件,修改入口Activity

 <activity 
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Dialog"
            android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.android5_8.MainActivity"
            android:label="主界面" >
        </activity>



import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.ImageIcon; import javax.swing.*; import java.lang.*; import java.sql.*; class Imagecanvas extends Canvas { Toolkit tool; Image Im; Imagecanvas() { setSize(360, 50); tool = getToolkit(); Im = tool.getImage("dj.jpg"); } public void paint(Graphics g) { g.drawImage(Im, 0, 0, 360, 50, this); } } class QqBorder1 extends JFrame implements ActionListener { Choice cho; QqBorder1() { super("QQ2010"); setSize(360, 260); setLocation(250, 120); setResizable(false); Container c = getContentPane(); FlowLayout layout = new FlowLayout(FlowLayout.CENTER); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); cho = new Choice(); cho.add("在线"); cho.add("隐身"); cho.add("离线"); cho.add("忙碌"); cho.add("请勿打扰"); Imagecanvas canvas1 = new Imagecanvas(); p1.setSize(360, 50); p1.add(canvas1); TextField username = new TextField(25); TextField password = new TextField(25); password.setEchoChar('*'); JButton btrystu = new JButton("登陆"); JButton sezhi = new JButton("设置"); JCheckBox check1 = new JCheckBox("记住密码"); JCheckBox check2 = new JCheckBox("自动登陆"); // p1.setSize(360, 200); JPanel p21 = new JPanel(); JPanel p22 = new JPanel(); p21.add(new JLabel("账号:")); p21.add(username); p21.add(new JLabel("<html><font color=blue>注册新账号</font></html>")); // p21.add(new JLabel(" ")); p21.add(new JLabel("密码:")); p21.add(password); p21.add(new JLabel("<html><font color=blue> 找 回 密 码 </font></html>")); p22.add(new JLabel("状态:")); p22.add(new JLabel(" ")); p22.add(cho); p22.add(check1); p22.add(check2); p22.add(new JLabel(" ")); p2.add(p21); p2.add(p22); p2.setBounds(100, 300, 200, 300); p2.setLayout(new GridLayout(2, 1, 10, 10)); p3.add(sezhi); p3.add(new JLabel(" ")); p3.add(btrystu); add(BorderLayout.NORTH, p1); add(BorderLayout.CENTER, p2); add(BorderLayout.SOUTH, p3); this.setVisible(true); } public void actionPerformed(ActionEvent e) { } } public class QqBorder { }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值