实例介绍
【实例简介】基于高德地图物流车辆轨迹APP,后端为 C#语言,webservice做接口
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import com.example.DbUtil.DBUtil;
import Decoder.BASE64Encoder;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class LoginActivity extends Activity {
private RadioGroup group;
private RadioButton rb_company,rb_person;
private Drawable bottomDrawableUnchecked;
private Drawable bottomDrawableChecked;
private Button bt_Exp,bt_Login;
private DBUtil dbUtil=new DBUtil();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
group = (RadioGroup) this.findViewById(R.id.radioGroup1);
rb_company = (RadioButton) this.findViewById(R.id.rb_company);
rb_person = (RadioButton) this.findViewById(R.id.rb_person);
bt_Exp=(Button)findViewById(R.id.bt_Exp);
bt_Login=(Button)findViewById(R.id.bt_Login);
//处理点击事件
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
});
rb_company.setOnCheckedChangeListener(rbCheckedChangeListener);
rb_person.setOnCheckedChangeListener(rbCheckedChangeListener);
rb_company.setChecked(true);
bottomDrawableUnchecked = getResources().getDrawable(R.drawable.unchecked);
bottomDrawableChecked = getResources().getDrawable(R.drawable.checked);
rb_company.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
public boolean onPreDraw() {
//如果draw的Bounds = rb1.getMeasuredWidth() 就代表已经设置过了。
if(!(bottomDrawableChecked.getBounds().right == rb_company.getMeasuredWidth())){
Rect rect = new Rect(0, 0, rb_company.getMeasuredWidth(), 10);
//设置图片边框
bottomDrawableUnchecked.setBounds(rect);
bottomDrawableChecked.setBounds(rect);
//设置图片
rb_company.setCompoundDrawables(null, null, null, bottomDrawableChecked);//第一个是选中状态
rb_person.setCompoundDrawables(null, null, null, bottomDrawableUnchecked);
//设置图片与文字的间距
rb_company.setCompoundDrawablePadding(10);
rb_person.setCompoundDrawablePadding(10);
}
return true;
}
});
/*增加命令按钮单机监听事件*/
bt_Login.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try
{
EditText et_userName=(EditText)findViewById(R.id.et_userName);
EditText et_password=(EditText)findViewById(R.id.et_password);
String userName=et_userName.getText().toString();
String password=et_password.getText().toString();
if(userName.equals("") || password.equals(""))
{
Toast.makeText(LoginActivity.this, "请输入用户名与密码", Toast.LENGTH_LONG).show();
return;
}
else
{
String pwd=digestMD5(password);
if(dbUtil.QueryUserInfo(userName, pwd)==1)
{
Intent intent=new Intent();
intent.setClass(LoginActivity.this,MainActivity.class);
startActivity(intent);
}
else if(dbUtil.QueryUserInfo(userName, pwd)==0)
{
Toast.makeText(LoginActivity.this, "用户名密码错误", Toast.LENGTH_LONG).show();
return;
}
else if(dbUtil.QueryUserInfo(userName, pwd)==2)
{
Toast.makeText(LoginActivity.this, "网络连接失败", Toast.LENGTH_LONG).show();
return;
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
});
bt_Exp.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(LoginActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
private CompoundButton.OnCheckedChangeListener rbCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
buttonView.setCompoundDrawables(null, null, null, bottomDrawableChecked);
}else{
buttonView.setCompoundDrawables(null, null, null, bottomDrawableUnchecked);
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static String digestMD5(String psw){
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
byte[] bbs = md.digest(psw.getBytes());
BASE64Encoder base64 = new BASE64Encoder();
String s = base64.encode(bbs);
return s;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
}
【实例截图】
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import com.example.DbUtil.DBUtil;
import Decoder.BASE64Encoder;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class LoginActivity extends Activity {
private RadioGroup group;
private RadioButton rb_company,rb_person;
private Drawable bottomDrawableUnchecked;
private Drawable bottomDrawableChecked;
private Button bt_Exp,bt_Login;
private DBUtil dbUtil=new DBUtil();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
group = (RadioGroup) this.findViewById(R.id.radioGroup1);
rb_company = (RadioButton) this.findViewById(R.id.rb_company);
rb_person = (RadioButton) this.findViewById(R.id.rb_person);
bt_Exp=(Button)findViewById(R.id.bt_Exp);
bt_Login=(Button)findViewById(R.id.bt_Login);
//处理点击事件
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
});
rb_company.setOnCheckedChangeListener(rbCheckedChangeListener);
rb_person.setOnCheckedChangeListener(rbCheckedChangeListener);
rb_company.setChecked(true);
bottomDrawableUnchecked = getResources().getDrawable(R.drawable.unchecked);
bottomDrawableChecked = getResources().getDrawable(R.drawable.checked);
rb_company.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
public boolean onPreDraw() {
//如果draw的Bounds = rb1.getMeasuredWidth() 就代表已经设置过了。
if(!(bottomDrawableChecked.getBounds().right == rb_company.getMeasuredWidth())){
Rect rect = new Rect(0, 0, rb_company.getMeasuredWidth(), 10);
//设置图片边框
bottomDrawableUnchecked.setBounds(rect);
bottomDrawableChecked.setBounds(rect);
//设置图片
rb_company.setCompoundDrawables(null, null, null, bottomDrawableChecked);//第一个是选中状态
rb_person.setCompoundDrawables(null, null, null, bottomDrawableUnchecked);
//设置图片与文字的间距
rb_company.setCompoundDrawablePadding(10);
rb_person.setCompoundDrawablePadding(10);
}
return true;
}
});
/*增加命令按钮单机监听事件*/
bt_Login.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try
{
EditText et_userName=(EditText)findViewById(R.id.et_userName);
EditText et_password=(EditText)findViewById(R.id.et_password);
String userName=et_userName.getText().toString();
String password=et_password.getText().toString();
if(userName.equals("") || password.equals(""))
{
Toast.makeText(LoginActivity.this, "请输入用户名与密码", Toast.LENGTH_LONG).show();
return;
}
else
{
String pwd=digestMD5(password);
if(dbUtil.QueryUserInfo(userName, pwd)==1)
{
Intent intent=new Intent();
intent.setClass(LoginActivity.this,MainActivity.class);
startActivity(intent);
}
else if(dbUtil.QueryUserInfo(userName, pwd)==0)
{
Toast.makeText(LoginActivity.this, "用户名密码错误", Toast.LENGTH_LONG).show();
return;
}
else if(dbUtil.QueryUserInfo(userName, pwd)==2)
{
Toast.makeText(LoginActivity.this, "网络连接失败", Toast.LENGTH_LONG).show();
return;
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
});
bt_Exp.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(LoginActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
private CompoundButton.OnCheckedChangeListener rbCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
buttonView.setCompoundDrawables(null, null, null, bottomDrawableChecked);
}else{
buttonView.setCompoundDrawables(null, null, null, bottomDrawableUnchecked);
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static String digestMD5(String psw){
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
byte[] bbs = md.digest(psw.getBytes());
BASE64Encoder base64 = new BASE64Encoder();
String s = base64.encode(bbs);
return s;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论