利用 ORMLite 数据库,ViewPager 等实现简易的登陆界面

这篇文章是对上一篇博客的扩展(《利用 ViewPager 等,实现带小圆球的图片滑动,并且只有第一次安装app时才出现欢迎界面(图片)》:http://blog.youkuaiyun.com/antimage08/article/details/50382680)。

利用 ORMLite 数据库记录用户注册账户的信息。在前文中有关于 ORMLite 的讲述及例子:http://blog.youkuaiyun.com/antimage08/article/details/49780047

修改上文 SecondActivity.java ,再扩充内容后得到效果如下,其中的 button 按钮的动画是 android5.0 以后新增的效果,如果不想要可以把背景换成 bg_board.xml :





修改后的 SecondActivity.java :

package com.android.circleforimage;

import java.sql.SQLException;
import java.util.List;

import com.android.circleforimagedatabase.ORMLiteDatabaseHelper;
import com.android.circleforimagedatabase.Record;
import com.android.circleforimageutils.Utils;
import com.j256.ormlite.dao.Dao;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SecondActivity extends Activity{

	private Dao<Record, Integer> mRecordDao; 
	private Button saveButton;
	private Record record;
	private EditText editName, editPassword;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second_activity);
		
		ORMLiteDatabaseHelper mDatabaseHelper = ORMLiteDatabaseHelper.getInstance(this);  
        mRecordDao = mDatabaseHelper.getRecordDao(); 
        record = new Record();
        
        saveButton = (Button)findViewById(R.id.save);
        saveButton.setAlpha(128);
        buttonClick();
        
	}
	
	private void buttonClick(){
		
		saveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				editName = (EditText)findViewById(R.id.editName);
				editPassword = (EditText)findViewById(R.id.editPassword);
				String str1 = editName.getText().toString();
				String str2 = editPassword.getText().toString();
				saveDataToDatabase(str1, str2);
				readDataFromDatabase();
			}
		});
	}
	
	private void saveDataToDatabase(String str1, String str2){
		
		record.setContent_Title(str1);
		record.setContent(str2);
		long time =System.currentTimeMillis();
		String dateFormat = Utils.TimeFormat(time);
		record.setDate(dateFormat);
		try {
			mRecordDao.createOrUpdate(record);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void readDataFromDatabase(){
		List<Record> mList = null;
		try {
			mList = mRecordDao.queryForAll();
			for (Record list : mList) {
				editName.setText(record.getContent_Title());
				editPassword.setText(record.getContent());
				Log.d("=============", list.toString());
				Toast.makeText(this, "您储存的信息为:" + list, Toast.LENGTH_LONG).show();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}



ORMLite 数据库部分,创建一个类,用于记录登陆时的数据;

Record.java :

package com.android.circleforimagedatabase;


import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "Record")
public class Record {

	public final static String CONTENT_ID = "content_id";
	public final static String CONTENT_TITLE = "content_title";
	public final static String CONTENT = "content";
	public final static String DATE = "date";

	public Record() {

	}

	public Record(String content_title, String content, String date) {
		this.content_title = content_title;
		this.content = content;
		this.date = date;
	}

	@DatabaseField(id = true, columnName = CONTENT_ID)
	public int content_id;

	@DatabaseField(columnName = CONTENT_TITLE)
	public String content_title;

	@DatabaseField(columnName = CONTENT)
	public String content;
	
	@DatabaseField(columnName = DATE)
	public String date;
	
	public int getRecordId() {
		return content_id;
	}

	public void setRecordId(int uid) {
		this.content_id = uid;
	}

	public String getContent_Title() {
		return content_title;
	}

	public void setContent_Title(String content_title) {
		this.content_title = content_title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	public String getDate(){
		return date;
	}
	
	public void setDate(String date){
		this.date = date;
	}

	@Override
	public String toString() {
		return "content_id:" + content_id + " 用户名:" + content_title +
				" 密码:" + content + " 注册日期:" + date;
	}
}




ORMLiteDatabaseHelper 类中修改的很少,可以和前面的 ORMLite 相印证;

ORMLiteDatabaseHelper.java :

package com.android.circleforimagedatabase;


import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class ORMLiteDatabaseHelper extends OrmLiteSqliteOpenHelper {
	
	public static ORMLiteDatabaseHelper mDatabaseHelper;
	
	private Dao<Record, Integer> mRecordDao = null;  
	  
    private final static String DataBase_NAME = "ormlite.db";  
    private final static int DataBase_VERSION = 1;
	
	public ORMLiteDatabaseHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion) {
		super(context, databaseName, factory, databaseVersion);
	}
	
	public static ORMLiteDatabaseHelper getInstance(Context context) {  
        if (mDatabaseHelper == null) {  
            mDatabaseHelper = new ORMLiteDatabaseHelper(context, DataBase_NAME,  
                    null, DataBase_VERSION);  
        }  
  
        return mDatabaseHelper;  
    } 
	
	@Override
	public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
		try {
			
			TableUtils.createTableIfNotExists(connectionSource, Record.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2, int arg3) {

	}
	
	/**
	 * ORMLite查插删改主要通过DAO。
	 * @return
	 */
	public Dao<Record, Integer> getRecordDao() {
		if (mRecordDao == null) {
			try {
				mRecordDao = getDao(Record.class);
			} catch (java.sql.SQLException e) {
				e.printStackTrace();
			}
		}

		return mRecordDao;
	}


	@Override
	public void close() {
		super.close();
		mRecordDao = null;
		
	}
	
}



在工具包(和前文的 Words.java 同一个包)中新建 Utils.java 类,用于存放常量,时间转换等;

Utils.java :

package com.android.circleforimageutils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Utils {
	public static String TimeFormat(long time) {

		SimpleDateFormat df = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH:mm:ss");
		Date date = new Date(time);
		String dateFormat = df.format(date);

		return dateFormat;
	}
}





second_activity.xml 主要用于登陆界面,该布局中的两个中文是因为在 strings.xml 中不易或者不能调到上下两个EditText 对齐,所以直接在布局中定义,按钮的点击效果是 android5.0 以后新增的。这里添加这个效果是让人感觉到已经点击的该按钮;

second_activity.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:gravity="center_horizontal"
        android:text="@string/text1"
        android:textStyle="italic"
        android:textColor="#f00"
        android:textSize="28sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#f00" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="8dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="用户名:"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/editName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:background="@drawable/bg_board2"
            android:ellipsize="start"
            android:hint="@string/text3"
            android:singleLine="true"
            android:textSize="12sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="密    码:"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/editPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_board2"
            android:ellipsize="start"
            android:padding="2dp"
            android:hint="@string/text5"
            android:inputType="numberPassword"
            android:singleLine="true"
            android:textSize="12sp" />
    </LinearLayout>

    <Button
        android:id="@+id/save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:textStyle="bold"
        android:background="@drawable/background_button"
        android:gravity="center"
        android:text="@string/text6"
        android:textSize="22sp" />

    
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:orientation="vertical" >

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_weight="3"
            android:background="@drawable/bg_board3" >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/text7"
                android:textColor="@android:color/darker_gray" />
        </ScrollView>

        <TextView 
            android:text="@string/text10"
            android:textSize="12sp"
            android:textColor="@android:color/darker_gray"
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:orientation="vertical" >
            <RadioGroup
                android:id="@+id/radioGroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <RadioButton
                    android:id="@+id/radioButton1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="@string/text8"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/radioButton2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/text9"
                    android:textSize="12sp" />
            </RadioGroup>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>





界面布局中所用到的 background_button.xml 在目录 res/drawable-v21 目录下:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="#0CC" >
    
    <item android:drawable="@drawable/bg_button" />
    
    <item 
        android:id="@android:id/mask"
        android:drawable="@drawable/bg_button" />

</ripple>




background_button.xml 中所用到的 bg_button.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:state_enabled="false"
        android:drawable="@drawable/bg_board" />
    
    <item
        android:state_pressed="true"
        android:drawable="@android:color/holo_blue_light" />
    
    <item 
        android:state_focused="true"
        android:drawable="@android:color/holo_blue_light" />
    
    <item android:drawable="@drawable/bg_board" />

</selector>




bg_button.xml 所用到的 bg_board.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <stroke android:width="4dp" android:color="#0ff" />
    <corners 
        android:topLeftRadius="5dp" 
        android:topRightRadius="5dp"
        android:bottomLeftRadius="5dp" 
        android:bottomRightRadius="5dp" />
    <solid android:color="#0ff" />
    
</shape>



界面布局中所用到的 bg_board2.xml :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <stroke android:width="2dp" android:color="#f0f" />
    <corners 
        android:topLeftRadius="4dp" 
        android:topRightRadius="4dp"
        android:bottomLeftRadius="4dp" 
        android:bottomRightRadius="4dp" />
    
</shape>



界面布局中所用到的 bg_board3.xml :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <stroke android:width="1dp" android:color="#456789" />
    <corners 
        android:topLeftRadius="4dp" 
        android:topRightRadius="4dp"
        android:bottomLeftRadius="4dp" 
        android:bottomRightRadius="4dp" />
    
</shape>



界面布局中所用到的 strings.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">CircleForImage</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    
    <string name="text1">注册信息:</string>
    <string name="text2">用户名:</string>
    <string name="text3">请输入用户名:</string>
    <string name="text4">密   码:</string>
    <string name="text5">请输入用户密码:</string>
    <string name="text6">保存</string>
    <string name="text8">不同意该协议</string>
    <string name="text9">同意该协议</string>
    <string name="text10">请认真阅读以上协议</string>
    <string name="text7">无论您的电脑操作水平如何,
        请注意被标记的警告和安全信息,同时请注意前言中的安全信息。
        无论您的电脑操作水平如何,请注意被标记的警告和安全信息,
        同时请注意前言中的安全信息。
        无论您的电脑操作水平如何,请注意被标记的警告和安全信息,
        同时请注意前言中的安全信息。无论您的电脑操作水平如何,
        请注意被标记的警告和安全信息,同时请注意前言中的安全信息。
        无论您的电脑操作水平如何,请注意被标记的警告和安全信息,
        同时请注意前言中的安全信息。
        无论您的电脑操作水平如何,请注意被标记的警告和安全信息,
        同时请注意前言中的安全信息。
        无论您的电脑操作水平如何,请注意被标记的警告和安全信息,
        同时请注意前言中的安全信息。
        无论您的电脑操作水平如何,请注意被标记的警告和安全信息,
        同时请注意前言中的安全信息。
        无论您的电脑操作水平如何,请注意被标记的警告和安全信息,
        同时请注意前言中的安全信息。
        无论您的电脑操作水平如何,请注意被标记的警告和安全信息,
        同时请注意前言中的安全信息。
        无论您的电脑操作水平如何,请注意被标记的警告和安全信息,
        同时请注意前言中的安全信息。
        无论您的电脑操作水平如何,请注意被标记的警告和安全信息,
        同时请注意前言中的安全信息。</string>

</resources>


内容概要:本文设计了一种基于PLC的全自动洗衣机控制系统内容概要:本文设计了一种,采用三菱FX基于PLC的全自动洗衣机控制系统,采用3U-32MT型PLC作为三菱FX3U核心控制器,替代传统继-32MT电器控制方式,提升了型PLC作为系统的稳定性与自动化核心控制器,替代水平。系统具备传统继电器控制方式高/低水,实现洗衣机工作位选择、柔和过程的自动化控制/标准洗衣模式切换。系统具备高、暂停加衣、低水位选择、手动脱水及和柔和、标准两种蜂鸣提示等功能洗衣模式,支持,通过GX Works2软件编写梯形图程序,实现进洗衣过程中暂停添加水、洗涤、排水衣物,并增加了手动脱水功能和、脱水等工序蜂鸣器提示的自动循环控制功能,提升了使用的,并引入MCGS组便捷性与灵活性态软件实现人机交互界面监控。控制系统通过GX。硬件设计包括 Works2软件进行主电路、PLC接梯形图编程线与关键元,完成了启动、进水器件选型,软件、正反转洗涤部分完成I/O分配、排水、脱、逻辑流程规划水等工序的逻辑及各功能模块梯设计,并实现了大形图编程。循环与小循环的嵌; 适合人群:自动化套控制流程。此外、电气工程及相关,还利用MCGS组态软件构建专业本科学生,具备PL了人机交互C基础知识和梯界面实现对洗衣机形图编程能力的运行状态的监控与操作。整体设计涵盖了初级工程技术人员。硬件选型、; 使用场景及目标:I/O分配、电路接线、程序逻辑设计及组①掌握PLC在态监控等多个方面家电自动化控制中的应用方法;②学习,体现了PLC在工业自动化控制中的高效全自动洗衣机控制系统的性与可靠性。;软硬件设计流程 适合人群:电气;③实践工程、自动化及相关MCGS组态软件与PLC的专业的本科生、初级通信与联调工程技术人员以及从事;④完成PLC控制系统开发毕业设计或工业的学习者;具备控制类项目开发参考一定PLC基础知识。; 阅读和梯形图建议:建议结合三菱编程能力的人员GX Works2仿真更为适宜。; 使用场景及目标:①应用于环境与MCGS组态平台进行程序高校毕业设计或调试与运行验证课程项目,帮助学生掌握PLC控制系统的设计,重点关注I/O分配逻辑、梯形图与实现方法;②为工业自动化领域互锁机制及循环控制结构的设计中类似家电控制系统的开发提供参考方案;③思路,深入理解PL通过实际案例理解C在实际工程项目PLC在电机中的应用全过程。控制、时间循环、互锁保护、手动干预等方面的应用逻辑。; 阅读建议:建议结合三菱GX Works2编程软件和MCGS组态软件同步实践,重点理解梯形图程序中各环节的时序逻辑与互锁机制,关注I/O分配与硬件接线的对应关系,并尝试在仿真环境中调试程序以加深对全自动洗衣机控制流程的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值