“Java 安卓版之View层”的版本间的差异
来自CloudWiki
(创建页面,内容为“==MVC简介== MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范。它是用一种…”) |
|||
第12行: | 第12行: | ||
*V即View视图是指用户看到并与之交互的界面。'''可以是命令行,也可以是html元素组成的网页界面,或者软件的客户端界面。'''。MVC的好处之一在于它能为应用程序处理很多不同的视图。在视图中其实没有真正的处理发生,它只是作为一种输出数据并允许用户操纵的方式。 | *V即View视图是指用户看到并与之交互的界面。'''可以是命令行,也可以是html元素组成的网页界面,或者软件的客户端界面。'''。MVC的好处之一在于它能为应用程序处理很多不同的视图。在视图中其实没有真正的处理发生,它只是作为一种输出数据并允许用户操纵的方式。 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | === | + | ===清单目录=== |
− | + | ||
+ | 首先,将清单目录的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{ | ||
− | + | 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(); | ||
+ | } | ||
− | + | } | |
− | |||
− | |||
− | |||
+ | }</nowiki> | ||
==运行结果== | ==运行结果== | ||
[[文件:Java8-6.png]] | [[文件:Java8-6.png]] | ||
==全部代码== | ==全部代码== | ||
− | <nowiki> | + | <nowiki> |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</nowiki> | </nowiki> |
2019年5月26日 (日) 13:05的最新版本
MVC简介
MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范。它是用一种业务逻辑、数据与界面显示分离的方法来组织代码,将众多的业务逻辑聚集到一个部件里面,在需要改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑,达到减少编码的时间。
MVC开始是存在于桌面程序中的,M是指业务模型,V是指用户界面,C则是控制器。
客户端功能
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(); } } }