码迷,mamicode.com
首页 > 其他好文 > 详细

用户登录

时间:2014-09-28 00:12:10      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   http   io   os   使用   ar   

用户登录的界面布局xml文档
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:stretchColumns="1"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitCenter" android:src="@drawable/logo" android:contentDescription="@string/hello"/> <TextView android:text="@string/welcome" android:id="@+id/TextView" android:textSize="@dimen/label_font_size" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="@dimen/title_padding"/> <!-- 输入用户名的行 --> <TableRow> <TextView android:text="@string/user_name" android:textSize="@dimen/label_font_size" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:id="@+id/userEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text"/> </TableRow> <!-- 输入密码的行 --> <TableRow> <TextView android:text="@string/user_pass" android:textSize="@dimen/label_font_size" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:text="" android:id="@+id/pwdEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword"/> </TableRow> <!-- 定义登录、取消按钮的行 --> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center"> <Button android:id="@+id/bnLogin" android:text="@string/login" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/bnCancel" android:text="@string/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </TableLayout>

  

用户点击登录按钮触发事件():
public class Login extends Activity { // 定义界面中两个文本框 EditText etName, etPass; // 定义界面中两个按钮 Button bnLogin, bnCancel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); // 获取界面中两个编辑框 etName = (EditText) findViewById(R.id.userEditText); etPass = (EditText) findViewById(R.id.pwdEditText); // 获取界面中的两个按钮 bnLogin = (Button) findViewById(R.id.bnLogin); bnCancel = (Button) findViewById(R.id.bnCancel); // 为bnCancal按钮的单击事件绑定事件监听器 bnCancel.setOnClickListener(new HomeListener(this)); bnLogin.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 执行输入校验 if (validate()) //① { // 如果登录成功 if (loginPro()) //② { // 启动Main Activity Intent intent = new Intent(Login.this , AuctionClientActivity.class); startActivity(intent); // 结束该Activity finish(); } else { DialogUtil.showDialog(Login.this , "用户名称或者密码错误,请重新输入!", false); } } } }); } private boolean loginPro() { // 获取用户输入的用户名、密码 String username = etName.getText().toString(); String pwd = etPass.getText().toString(); JSONObject jsonObj; try { jsonObj = query(username, pwd); // 如果userId 大于0 if (jsonObj.getInt("userId") > 0) { return true; } } catch (Exception e) { DialogUtil.showDialog(this , "服务器响应异常,请稍后再试!", false); e.printStackTrace(); } return false; } // 对用户输入的用户名、密码进行校验 private boolean validate() { String username = etName.getText().toString().trim(); if (username.equals("")) { DialogUtil.showDialog(this, "用户账户是必填项!", false); return false; } String pwd = etPass.getText().toString().trim(); if (pwd.equals("")) { DialogUtil.showDialog(this, "用户口令是必填项!", false); return false; } return true; } // 定义发送请求的方法 private JSONObject query(String username, String password) throws Exception { // 使用Map封装请求参数 Map<String, String> map = new HashMap<String, String>(); map.put("user", username); map.put("pass", password); // 定义发送请求的URL String url = HttpUtil.BASE_URL + "login.jsp"; // 发送请求 return new JSONObject(HttpUtil.postRequest(url, map)); } }

  

当用户的用户名,密码输入不正确时,对话框会提示登录失败。因为本系统要经常提示对话框,所以专门把它定义成一个独立的类:
public class DialogUtil { // 定义一个显示消息的对话框 public static void showDialog(final Context ctx , String msg , boolean goHome) { // 创建一个AlertDialog.Builder对象 AlertDialog.Builder builder = new AlertDialog.Builder(ctx) .setMessage(msg).setCancelable(false); if(goHome) { builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent i = new Intent(ctx , AuctionClientActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); ctx.startActivity(i); } }); } else { builder.setPositiveButton("确定", null); } builder.create().show(); } // 定义一个显示指定组件的对话框 public static void showDialog(Context ctx , View view) { new AlertDialog.Builder(ctx) .setView(view).setCancelable(false) .setPositiveButton("确定", null) .create() .show(); } }

  

登录成功,系统启动主界面AuctionClientActivity,可通过ListView进入各个功能。
AuctionClientActivity界面的布局文件分为两个,手机界面和平板电脑界面
下面是手机界面,主要的组件是AuctionListFragment:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:layout_width="match_parent"
	android:layout_height="match_parent">
	<!-- 添加一个Fragment -->
	<fragment
		android:name="org.crazyit.auction.client.AuctionListFragment"
		android:id="@+id/auction_list"
		android:layout_width="match_parent"
		android:layout_height="match_parent"/>
</LinearLayout>

下面是手机界面,主要的组件是AuctionListFragment,还包含一个FrameLayout容器,该容器用于装载对应功能的Fragment组件

<?xml version="1.0" encoding="utf-8"?>
<!-- 定义一个水平排列的LinearLayout,并指定使用中等分隔条 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginLeft="16dp"
  android:layout_marginRight="16dp"
  android:divider="?android:attr/dividerHorizontal"
  android:showDividers="middle">
<!-- 添加一个Fragment -->
<fragment
  android:name="org.crazyit.auction.client.AuctionListFragment"
  android:id="@+id/auction_list"
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="1" />
<!-- 添加一个FrameLayout容器 -->
<FrameLayout
  android:id="@+id/auction_detail_container"
  android:layout_width="0dp"
  android:paddingLeft="10dp"
  android:layout_height="match_parent"
  android:layout_weight="3" />
</LinearLayout>

 

因为上面两种显示界面都包含相同名字的布局文件(所处的文件夹不同),AuctionListFragment会根据屏幕尺寸来加载不同目录下的界面布局文件。

下面是AuctionListFragment代码:

public class AuctionClientActivity extends Activity
	implements Callbacks
{
	// 定义一个旗标,用于标识该应用是否支持大屏幕
	private boolean mTwoPane;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// 指定加载R.layout.activity_main对应的界面布局文件
		// 但实际上该应用会根据屏幕分辨率家在不同的界面布局文件
		setContentView(R.layout.activity_main);
		// 如果加载的界面布局文件中包含ID为book_detail_container的组件
		if (findViewById(R.id.auction_detail_container) != null)
		{
			mTwoPane = true;
			((AuctionListFragment) getFragmentManager()
				.findFragmentById(R.id.auction_list))
				.setActivateOnItemClick(true);
		}
	}
	@Override
	public void onItemSelected(Integer id , Bundle bundle)
	{
		if (mTwoPane)
		{
			Fragment fragment = null;
			switch ((int) id)
			{
				// 查看竞得物品
				case 0:
					// 创建ViewItemFragment
					fragment = new ViewItemFragment();
					// 创建Bundle,准备向Fragment传入参数
					Bundle arguments = new Bundle();
					arguments.putString("action", "viewSucc.jsp");
					// 向Fragment传入参数
					fragment.setArguments(arguments);
					break;
				// 浏览流拍物品
				case 1:
					// 创建ViewItemFragment
					fragment = new ViewItemFragment();
					// 创建Bundle,准备向Fragment传入参数
					Bundle arguments2 = new Bundle();
					arguments2.putString("action", "viewFail.jsp");
					// 向Fragment传入参数
					fragment.setArguments(arguments2);
					break;
				// 管理物品种类
				case 2:
					// 创建ManageKindFragment
					fragment = new ManageKindFragment();
					break;
				// 管理物品
				case 3:
					// 创建ManageItemFragment
					fragment = new ManageItemFragment();
					break;
				// 浏览拍卖物品(选择物品种类)
				case 4:
					// 创建ChooseKindFragment
					fragment = new ChooseKindFragment();
					break;
				// 查看自己的竞标
				case 5:
					// 创建ViewBidFragment
					fragment = new ViewBidFragment();
					break;
				case ManageItemFragment.ADD_ITEM:
					fragment = new AddItemFragment();
					break;
				case ManageKindFragment.ADD_KIND:
					fragment = new AddKindFragment();
					break;
				case ChooseKindFragment.CHOOSE_ITEM:
					fragment = new ChooseItemFragment();
					Bundle args = new Bundle();
					args.putLong("kindId", bundle.getLong("kindId"));
					fragment.setArguments(args);
					break;
				case ChooseItemFragment.ADD_BID:
					fragment = new AddBidFragment();
					Bundle args2 = new Bundle();
					args2.putInt("itemId", bundle.getInt("itemId"));
					fragment.setArguments(args2);
					break;
			}			
			// 使用fragment替换auction_detail_container容器当前显示的Fragment
			getFragmentManager().beginTransaction()
				.replace(R.id.auction_detail_container, fragment)
				.addToBackStack(null).commit();
		}
		else
		{
			Intent intent = null;
			switch ((int) id)
			{
				// 查看竞得物品
				case 0:
					// 启动ViewItem Activity
					intent = new Intent(this, ViewItem.class);
					// action属性为请求的Servlet地址。
					intent.putExtra("action", "viewSucc.jsp");
					startActivity(intent);
					break;
				// 浏览流拍物品
				case 1:
					// 启动ViewItem Activity
					intent = new Intent(this, ViewItem.class);
					// action属性为请求的Servlet的URL。
					intent.putExtra("action", "viewFail.jsp");
					startActivity(intent);
					break;
				// 管理物品种类
				case 2:
					// 启动ManageKind Activity
					intent = new Intent(this, ManageKind.class);
					startActivity(intent);
					break;
				// 管理物品
				case 3:
					// 启动ManageItem Activity
					intent = new Intent(this, ManageItem.class);
					startActivity(intent);
					break;
				// 浏览拍卖物品(选择物品种类)
				case 4:
					// 启动ChooseKind Activity
					intent = new Intent(this, ChooseKind.class);
					startActivity(intent);
					break;
				// 查看自己的竞标
				case 5:
					// 启动ViewBid Activity
					intent = new Intent(this, ViewBid.class);
					startActivity(intent);
					break;
			}
		}
	}
}
如果程序在手机上运行,AuctionListFragment提供一个的ListView,当用户点击不同的列表项时,程序将会启动不同的activity;
如果程序在平板电脑上运行,AuctionListFragment提供一个的ListView和一个FrameLayout容器,当用户点击不同的列表项时,程序通过FrameLayout装载不同fragment;
bubuko.com,布布扣

  

 

  

用户登录

标签:des   android   style   blog   http   io   os   使用   ar   

原文地址:http://www.cnblogs.com/zhujiabin/p/3997397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!