“Java 安卓版之View层”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
(创建页面,内容为“==MVC简介== MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范。它是用一种…”)
 
 
第12行: 第12行:
  
 
*V即View视图是指用户看到并与之交互的界面。'''可以是命令行,也可以是html元素组成的网页界面,或者软件的客户端界面。'''。MVC的好处之一在于它能为应用程序处理很多不同的视图。在视图中其实没有真正的处理发生,它只是作为一种输出数据并允许用户操纵的方式。
 
*V即View视图是指用户看到并与之交互的界面。'''可以是命令行,也可以是html元素组成的网页界面,或者软件的客户端界面。'''。MVC的好处之一在于它能为应用程序处理很多不同的视图。在视图中其实没有真正的处理发生,它只是作为一种输出数据并允许用户操纵的方式。
===新建窗体===
 
<nowiki>import java.awt.*;
 
import javax.swing.*;
 
public class UserInfo extends JFrame{
 
public UserInfo() {
 
super("电商购物平台-用户信息");
 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭时操作
 
setResizable(false);//窗口大小不能改变
 
setSize(300,100);//设置窗口大小
 
setVisible(true);//设置窗口可见性
 
}
 
public static void main(String[] args) {
 
UserInfo userInfo=new UserInfo();
 
}
 
}</nowiki>
 
  
===添加组件===
+
===清单目录===
在UserInfo类中添加按钮、组件作为成员变量:
+
 
 +
首先,将清单目录的acticity改为LoginActivity
 +
 
 +
AndroidManifest.xml:
 +
 
 +
<nowiki><?xml version="1.0" encoding="utf-8"?>
 +
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 +
    package="com.example.administrator.myapplication">
 +
 
 +
    <application
 +
        android:allowBackup="true"
 +
        android:icon="@mipmap/ic_launcher"
 +
        android:label="@string/app_name"
 +
        android:supportsRtl="true"
 +
        android:theme="@style/AppTheme">
 +
        <activity android:name=".LoginAcitivity">
 +
            <intent-filter>
 +
                <action android:name="android.intent.action.MAIN" />
 +
 
 +
                <category android:name="android.intent.category.LAUNCHER" />
 +
            </intent-filter>
 +
        </activity>
 +
 
 +
 
 +
 
 +
    </application>
 +
 
 +
</manifest></nowiki>
 +
 
 +
===新建布局文件===
 +
Login_layout.xml:
 +
 
 +
<nowiki><?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">
 +
 
 +
    <TextView
 +
        android:layout_width="match_parent"
 +
        android:layout_height="wrap_content"
 +
        android:text="用户名" />
 +
 
 +
    <EditText
 +
        android:id="@+id/username"
 +
        android:layout_width="match_parent"
 +
        android:layout_height="wrap_content" />
 +
 
 +
    <TextView
 +
        android:layout_width="match_parent"
 +
        android:layout_height="wrap_content"
 +
        android:text="密码" />
 +
 
 +
    <EditText
 +
        android:id="@+id/password"
 +
        android:layout_width="match_parent"
 +
        android:layout_height="wrap_content"
 +
        android:inputType="textPassword" />
 +
 
 +
    <Button
 +
        android:layout_width="wrap_content"
 +
        android:layout_height="wrap_content"
 +
        android:onClick="loginClick"
 +
        android:text="登录"
 +
        android:layout_gravity="center"/>
 +
 
 +
</LinearLayout></nowiki>
 +
 
 +
===新建登录的Activity===
 +
LoginActivity:
 +
 
 +
<nowiki>package com.example.administrator.myapplication;
 +
 
 +
import android.app.Activity;
 +
import android.content.Intent;
 +
import android.os.Bundle;
 +
import android.util.Log;
 +
import android.view.View;
 +
import android.widget.EditText;
 +
import android.widget.Toast;
 +
 
 +
import com.example.administrator.myapplication.service.UserinfoService;
 +
 
 +
/**
 +
* Created by Administrator on 2017/1/7.
 +
*/
 +
public class LoginAcitivity  extends Activity{
  
<nowiki>private JLabel labelUser=new JLabel("用户登录",JLabel.CENTER);
+
    private UserinfoService service=new UserinfoService();
private JLabel labelPwd= new JLabel("登录密码",JLabel.CENTER);//“登录密码”标签
 
private JTextField txtUserID = new JTextField(16);//“登录账号”文本框
 
private JPasswordField txtPassword=new JPasswordField(16);//密码框
 
private JButton btnLogon =new JButton("登陆");//登录按钮
 
private JButton btnSet = new JButton("注册");//重置按钮</nowiki>
 
  
===将组件添加到面板上===
+
    private EditText etUsername;
在框架中,添加第一块面板。
+
    private EditText etPassword;
 +
    @Override
 +
    protected void onCreate(Bundle savedInstanceState) {
 +
        super.onCreate(savedInstanceState);
 +
        setContentView(R.layout.login_layout);
 +
        etUsername= (EditText) findViewById(R.id.username);
 +
        etPassword= (EditText) findViewById(R.id.password);
 +
    }
  
  <nowiki>//A.创建第一个面板,内部采用网格布局,在其中添加用户登录、用户密码控件
+
    public void loginClick(View v)
        JPanel centerPane=new JPanel();
+
    {
    centerPane.setLayout(new GridLayout(2,2));//步骤1:创建网格布局 
+
        //System.out.println("Click");
centerPane.add(labelUser);//步骤2:将组件添加到面板上
+
      // Log.d("login","hahahah");
centerPane.add(txtUserID); //centerPane.add(labelUser2);
 
centerPane.add(labelPwd);
 
centerPane.add(txtPassword);
 
this.add(centerPane, BorderLayout.CENTER);
 
   
 
</nowiki>
 
在框架中,添加第二块面板。
 
  
<nowiki>//B.创建第二个面板,内部采用流式布局,在其中添加用户登录和重置按钮
+
        String username=etUsername.getText().toString();
    JPanel southPane=new JPanel();  
+
        String password=etPassword.getText().toString();
    southPane.setLayout(new FlowLayout());
+
        try{
    southPane.add(btnLogon);
+
            service.login(username,password);
    southPane.add(btnSet);
+
            Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();
    this.add(southPane, BorderLayout.SOUTH);</nowiki>
+
            Intent intent=new Intent(getApplicationContext(),MainContent.class);
 +
            startActivity(intent);
 +
        }
 +
        catch (Exception e)
 +
        {
 +
            Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
 +
        }
  
===测试程序,在main方法中添加语句。===
+
    }
<nowiki>public static void main(String[] args) {
 
UserInfo userInfo=new UserInfo();
 
}</nowiki>
 
  
 +
}</nowiki>
 
==运行结果==
 
==运行结果==
 
[[文件:Java8-6.png]]
 
[[文件:Java8-6.png]]
 
==全部代码==
 
==全部代码==
  <nowiki>package view;
+
  <nowiki>
 
 
import java.awt.*;
 
import javax.swing.*;
 
 
 
 
 
public class UserInfo extends JFrame{
 
/***
 
*/
 
private static final long serialVersionUID = 1L;
 
private JLabel labelUser=new JLabel("用户登录",JLabel.CENTER);
 
private JLabel labelPwd= new JLabel("登录密码",JLabel.CENTER);//“登录密码”标签
 
private JTextField txtUserID = new JTextField(16);//“登录账号”文本框
 
private JPasswordField txtPassword=new JPasswordField(16);//密码框
 
//private JLabel labelUser2=new JLabel("@126.com",JLabel.CENTER);
 
    private JButton btnLogon =new JButton("登陆");//登录按钮
 
private JButton btnSet = new JButton("注册");//重置按钮
 
 
public UserInfo() {
 
super("电商购物平台-用户信息");
 
 
//A.创建第一个面板,内部采用网格布局,在其中添加用户登录、用户密码控件
 
JPanel centerPane=new JPanel();
 
    centerPane.setLayout(new GridLayout(2,2));//步骤1:创建网格布局 
 
centerPane.add(labelUser);//步骤2:将组件添加到面板上
 
centerPane.add(txtUserID);
 
//centerPane.add(labelUser2);
 
centerPane.add(labelPwd);
 
centerPane.add(txtPassword);
 
this.add(centerPane, BorderLayout.CENTER);
 
   
 
//B.创建第二个面板,内部采用流式布局,在其中添加用户登录和重置按钮
 
    JPanel southPane=new JPanel();   
 
    southPane.setLayout(new FlowLayout());
 
    southPane.add(btnLogon);
 
    southPane.add(btnSet);
 
    this.add(southPane, BorderLayout.SOUTH);
 
   
 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭时操作
 
setResizable(false);//窗口大小不能改变
 
setSize(300,100);//设置窗口大小
 
setVisible(true);//设置窗口可见性
 
}
 
public static void main(String[] args) {
 
UserInfo userInfo=new UserInfo();
 
}
 
 
}
 
 
</nowiki>
 
</nowiki>

2019年5月26日 (日) 13:05的最新版本

MVC简介

MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范。它是用一种业务逻辑、数据与界面显示分离的方法来组织代码,将众多的业务逻辑聚集到一个部件里面,在需要改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑,达到减少编码的时间。

MVC开始是存在于桌面程序中的,M是指业务模型,V是指用户界面,C则是控制器。

Java8-1.png

客户端功能

Java2-36.png

View层代码

  • V即View视图是指用户看到并与之交互的界面。可以是命令行,也可以是html元素组成的网页界面,或者软件的客户端界面。。MVC的好处之一在于它能为应用程序处理很多不同的视图。在视图中其实没有真正的处理发生,它只是作为一种输出数据并允许用户操纵的方式。

清单目录

首先,将清单目录的acticity改为LoginActivity

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".LoginAcitivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>



    </application>

</manifest>

新建布局文件

Login_layout.xml:

<?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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="用户名" />

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="密码" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="loginClick"
        android:text="登录"
        android:layout_gravity="center"/>

</LinearLayout>

新建登录的Activity

LoginActivity:

package com.example.administrator.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.myapplication.service.UserinfoService;

/**
 * Created by Administrator on 2017/1/7.
 */
public class LoginAcitivity  extends Activity{

    private UserinfoService service=new UserinfoService();

    private EditText etUsername;
    private EditText etPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);
        etUsername= (EditText) findViewById(R.id.username);
        etPassword= (EditText) findViewById(R.id.password);
    }

    public void  loginClick(View v)
    {
        //System.out.println("Click");
       // Log.d("login","hahahah");

        String username=etUsername.getText().toString();
        String password=etPassword.getText().toString();
        try{
            service.login(username,password);
            Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(getApplicationContext(),MainContent.class);
            startActivity(intent);
        }
        catch (Exception e)
        {
            Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
        }

    }

}

运行结果

Java8-6.png

全部代码