执行的命令是
nox_adb.exe connect 127.0.0.1:62001
点下图中绿箭头,会出现”Samsung Nexus”,如果不出现,在dos窗口中再执行一次” nox_adb.exe connect 127.0.0.1:62001”
点“Proceed without Instant Run”不用闪电安装功能
在android视图中修改action_main.xml
将相对布局RelativeLayout改成线性布局LinearLayout,设置方向orientation.
修改文本,字号
增加button
修改MainActivity.java
public class MainActivity extends AppCompatActivity {
//程序启动时执行onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到btn
Button btn = (Button) findViewById(R.id.registerBtn);
//给btn设置事件监听
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//显示文本
Toast.makeText(MainActivity.this, "注册", Toast.LENGTH_LONG).show();
}
});
}
}
到\app\build\outputs\apk文件夹下找到apk,拷贝到\pdServer\src\main\webapp\文件夹中。
制作apk.html
apk
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//启线程
MyThread myThread=new MyThread();
myThread.start();
}
//内部类
class MyThread extends Thread
{
@Override
public void run() {
try{
//停3秒
sleep(3000);
//跳到LoginActiviyt
//从mainActivity跳到LoginActivity
Intent intent=new Intent(MainActivity.this,LoginActivity.class);
//开始跳到loginActivity
startActivity(intent);
}catch (Exception e)
{
e.printStackTrace();
}
}
}
}
在activity_main.xml中增加 background属性设置背景
1.创建LoginActivity
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//单击登录界面中的注册按钮跳到注册界面
Button registerBtn= (Button) findViewById(R.id.login_register_btn);
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//跳到注册界面
Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(intent);
}
});
}
}
Res下的文件名以数字开头或大写,重新build就报错。
需求
1.创建RegisterActivity
2.拷贝activity_login.xml中的内容到activity_register.xml中。修改id,增加确认密码,手机号,邮箱
3.登录中单击按钮跳到到RegisterActivity
在LoginActivity增加下面代码
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
}
1. 查看源码 pdServer\src\main\java\com\pd\controller\UserController.java
@RequestMapping("/user/register.html")
@ResponseBody
//http://ip/user/register.html
//?username=180501&password=123456&phone=13691481001&email=1@qq.com
public PdResult register(PdUser ajiaUser)
throws Exception
{
PdResult ajiaResult=new PdResult();
ajiaResult.setStatus(500);
String password=ajiaUser.getPassword();
password=DesUtil.encrypt(password, "88889999");
password=MD5Encrypt.MD5Encode(password);
ajiaUser.setPassword(password);
String username=ajiaUser.getUsername();
PdUser dbPdUser=userService
.selectByUsername(username);
if (dbPdUser==null)
{
int row=userService.insert(ajiaUser);
if (row>=1)
{
ajiaResult.setStatus(200);
}
}
return ajiaResult;
}
2. 浏览器测试
http://ip/user/register.html?username=180501&password=123456&phone=13691481001&email=1@qq.com
第一次访问,服务器返回
{"status":200,"msg":null,"data":null}
再刷新url,服务器返回
{"status":500,"msg":null,"data":null}
3. 查看数据库有没有注册成功
执行下面select查询最新用户
SELECT * FROM pd_user ORDER BY id desc
把两个jar加到编译路径中有两种方 法
第一种方法
在下图中点击ok
第二种方法
1,创建实体类
//{"status":200,"msg":null,"data":null}
public class ServerResult implements Serializable {
private int status;
private String msg;
private Object data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
在manifests\AndroidManifest.xml内增加权限
定义常量
public class PdApplication {
final static String serverIP="http://192.168.221.194";
}
2.联网发数据
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final Button registerBtn = (Button) findViewById(R.id.register_registerButton);
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//1.找到5个控件
EditText usernameEt = (EditText) findViewById(R.id.register_usernameEditText);
EditText passwordEt = (EditText) findViewById(R.id.register_paswordEditText);
EditText confirmPasswordEt = (EditText) findViewById(R.id.register_confirm_paswordEditText);
EditText phoneEt = (EditText) findViewById(R.id.register_phoneEditText);
EditText emailEt = (EditText) findViewById(R.id.register_emailEditText);
//2.接收信息
String username = usernameEt.getText().toString();
String passowrd = passwordEt.getText().toString();
String confirmPassword = confirmPasswordEt.getText().toString();
String phone = phoneEt.getText().toString();
String email = emailEt.getText().toString();
//3.判断信息
if (passowrd.equals(confirmPassword)) {
//4. 联网
//4.1 拼出url
String url = PdApplication.serverIP+"/user/register.html?" +
"username=" + username +
"&password=" + passowrd +
"&phone=" + phone +
"&email=" + email;
//4.2 发数据
MyListener myListener = new MyListener();
MyErrorListener myErrorListener = new MyErrorListener();
StringRequest request = new StringRequest(url, myListener, myErrorListener);
RequestQueue requestQueue = Volley.newRequestQueue(RegisterActivity.this);
requestQueue.add(request);
} else {
Toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//内部类
class MyListener implements Response.Listener {
@Override
public void onResponse(String s) {
Log.i("pd", s);
//4.3 收数据
//4.4 把json转成对象
ServerResult result = JSON.parseObject(s, ServerResult.class);
if (result.getStatus() == 200) {
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_LONG).show();
}
}
}
class MyErrorListener implements Response.ErrorListener {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.i("pd", volleyError.getMessage());
}
}
}
1. 查看源码 pdServer\src\main\java\com\pd\controller\UserController.java
//http://localhost:8090/user/login.html?username=b&password=123457
@RequestMapping("/user/login.html")
@ResponseBody
public PdResult Login
(String username,String password) throws Exception
{
password=DesUtil.encrypt(password, "88889999");
password=MD5Encrypt.MD5Encode(password);
PdResult ajiaResult=new PdResult();
ajiaResult.setStatus(500);
PdUser dbPdUser=userService
.selectByUsername(username);
if (dbPdUser!=null &&
password.equals(dbPdUser.getPassword()))
{
ajiaResult.setStatus(200);
}
return ajiaResult;
}
2. 浏览器测试
http://192.168.221.194/user/login.html?username=180601&password=123456
LoginActivity.oncreate()中增加如下代码
final Button loginButton = (Button)
findViewById(R.id.login_loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//1.找到2个控件
EditText usernameEditText = (EditText)
findViewById(R.id.login_usernameEditText);
EditText passwordEditText = (EditText)
findViewById(R.id.login_paswordEditText);
//2.接收信息
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
//3.判断信息
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(LoginActivity.this, "用户名或密码为空", Toast.LENGTH_LONG).show();
} else {
//4.联网发送
String url = PdApplication.serverIP+"/user/login.html?" +
"username=" + username +
"&password=" + password;
StringRequest request = new StringRequest(url, new Response.Listener() {
@Override
public void onResponse(String s) {
ServerResult result = JSON.parseObject(s, ServerResult.class);
if (result.getStatus() == 200) {
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(LoginActivity.this);
requestQueue.add(request);
}
}
});
要拷贝的res文件夹中有ic_launcher.png等文件。
没有activity_main.xml, activity_register.xml, activity_login.xmlM平in
创建java Class MainFragment
加载main_framgent,只出button
public class MainFragmentActivity
extends FragmentActivity{
//只有一个参数
@Override
protected void onCreate
(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_fragment);
}
}
清单文件注册
if (result.getStatus() == 200) {
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
try {//保存用户
Intent intent = new Intent(LoginActivity.this, MainFragmentActivity.class);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, "pd.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table pd_user ("
+ "username varchar(50)) ";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class UserDAO {
public long insert(Activity activity, String username) {
long result = -1;
DatabaseHelper databaseHelper = new DatabaseHelper(activity);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("username", username);
String table = "pd_user";
String nullColumnHack = null;
//添加数据的id
result = db.insert(table, nullColumnHack, values);
db.close();
return result;
}
}
if (result.getStatus() == 200) {
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
try {//保存用户
UserDAO userDAO = new UserDAO();
userDAO.insert(LoginActivity.this, username);
Intent intent = new Intent(LoginActivity.this, MainFragmentActivity.class);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
使用原生模拟器执行android-sdk-windows\platform-tools下的adb
使用夜神模拟器执行D:\Program Files\Nox\bin>文件夹下的adb
在 dos中切换到文件夹中执行下面的命令
adb shell
root@android:/ # ls
data
etc
root@android:/ # cd data
root@android:/data # ls
data
root@android:/data # cd data
root@android:/data/data # ls
com.pd.pdmobilestore
root@android:/data/data # cd com.pd.pdmobilestore
root@android:/data/data/com.pd.pdmobilestore # ls
app_webview
cache
databases
lib
root@android:/data/data/com.pd.pdmobilestore # cd databases
root@android:/data/data/com.pd.pdmobilestore/databases # ls
pd.db
pd.db-journal
root@android:/data/data/com.pd.pdmobilestore/databases # sqlite3 pd.db
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .table
android_metadata pd_user
sqlite> select * from pd_user;
180611
sqlite> .quit
root@android:/data/data/com.pd.pdmobilestore/databases # exit
D:\Program Files\Nox\bin>
userDao增加query
public int query(Activity activity) {
DatabaseHelper databaseHelper = new DatabaseHelper(activity);
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String[] args = null;
//cursor 相当于jdbc中的resultSet
Cursor cursor = db.rawQuery("select * from pd_user", args);
if (cursor != null) {
int count = cursor.getCount();
return count;
}
cursor.close();
return 0;
}
2.MainActivity修改代码
class ToLoginThread extends Thread {
@Override
public void run() {
try {
sleep(3000);
//从数据库中查询用户是否登录
UserDAO userDAO = new UserDAO();
int count = userDAO.query(MainActivity.this);
if (count >= 1) {
//已经登录过,直接显示主界面
Intent intent = new Intent(MainActivity.this, MainFragmentActivity.class);
startActivity(intent);
} else {
//没登录,跳到loginActivity
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Fragment是可重用的界面
创建三个fragment IndexFragment,MessageFragment,MeFragment
添加联网权限
创建IndexFragment
//fragment类是v4包
public class IndexFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//把fragment_index.xml转成view
/*getActiviyt()返回的是mainFragmentActivity*/
View view=View.inflate(getActivity(),R.layout.fragment_index,null);
//找到webView
WebView webView= (WebView) view.findViewById(R.id.webView);
//单击超连接,打开的是手机的浏览器,不能打开浏览器
webView.setWebViewClient(new WebViewClient());
//用webView加载网页
String url=PdApplication.serverIP+"/mobileIndex.html";
webView.loadUrl(url);
WebSettings webSettings=webView.getSettings();
//允许webview执行js
webSettings.setJavaScriptEnabled(true);
//返回view
return view;
}
}
public class MainActivity extends FragmentActivity {
IndexFragment indexFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_fragment);
//显示storefragment
FragmentManager fragmentManager=getSupportFragmentManager();
//transaction事务:包含多个动作,有一个失败,都失败
FragmentTransaction transaction=fragmentManager.beginTransaction();
indexFragment=new IndexFragment();
transaction.add(R.id.fragment_container,indexFragment);
transaction.show(indexFragment);
//add()和show()有一个失败了,出异常,不执行commit()
transaction.commit();
}
}
创建messageFragment
public class MessageFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=View.inflate
(getActivity(),
R.layout.fragment_message,
null);
return view;
}
}
创建meFragment
public class MeFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=View.inflate
(getActivity(),
R.layout.fragment_me,
null);
return view;
}
}
修改MainFragmentActivity
//管理商城,消息,我3个fragment
//所有的activity必须在androidManifest.xml中注册
public class MainFragmentActivity
extends FragmentActivity{
IndexFragment storeFragment;
MessageFragment messageFragment;
MeFragment meFragment;
//store,message,me
Fragment[] fragments=new Fragment[3];
//storebtn,messageBtn,meBtn
Button[] buttons=new Button[3];
//正在显示的fragment下标
int currentFragmentIndex=0;
//单击的btn下标
int clickBtnIndex=0;
//只有一个参数
@Override
protected void onCreate
(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_fragment);
storeFragment=new IndexFragment();
//得到fragment管理对象
//v4包
FragmentManager manager=getSupportFragmentManager();
//开始事务
FragmentTransaction transaction=manager.beginTransaction();
//1,添置fragment
//fragment_container是主界面里的一个linearLayout
//用linearLayout显示fragment
transaction.add(R.id.fragment_container,storeFragment);
//2,显示fragment
transaction.show(storeFragment);
//add()和show()都执行成功提交事务
transaction.commit();
//创建message,me fragment
messageFragment=new MessageFragment();
meFragment=new MeFragment();
//把三个fragment放到数组
fragments[0]=storeFragment;
fragments[1]=messageFragment;
fragments[2]=meFragment;
//找到三个button
buttons[0]= (Button) findViewById
(R.id.btn_main_fragment_store);
buttons[1]= (Button) findViewById
(R.id.btn_main_fragment_message);
buttons[2]= (Button) findViewById
(R.id.btn_main_fragment_me);
//为三个button设置事件监听
MyBtnListener listener=new MyBtnListener();
buttons[0].setOnClickListener(listener);
buttons[1].setOnClickListener(listener);
buttons[2].setOnClickListener(listener);
buttons[currentFragmentIndex].setSelected(true);
}
class MyBtnListener implements
View.OnClickListener
{
@Override
public void onClick(View v) {
//判断单击的是第几个按钮
switch (v.getId())
{
case R.id.btn_main_fragment_store:
clickBtnIndex=0;
break;
case R.id.btn_main_fragment_message:
clickBtnIndex=1;
break;
case R.id.btn_main_fragment_me:
clickBtnIndex=2;
break;
}
//判断单击的是不是当前按钮
if (clickBtnIndex!=currentFragmentIndex) {
//得到fragment管理对象
FragmentManager manager=getSupportFragmentManager();
//开始事务
FragmentTransaction transaction=manager.beginTransaction();
//显示新的fragment
Fragment showFragment=fragments[clickBtnIndex];
if (showFragment.isAdded()==false) {
transaction.add(R.id.fragment_container,
showFragment);
}
transaction.show(showFragment);
//隐藏旧的fragment
Fragment hideFragment=fragments[currentFragmentIndex];
transaction.hide(hideFragment);
//提交事务
transaction.commit();
buttons[clickBtnIndex].setSelected(true);
buttons[currentFragmentIndex].setSelected(false);
currentFragmentIndex=clickBtnIndex;
}
}
}
}
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
String url = "http://172.60.50.123:8020/html4/jd.html";
webView.loadUrl(url);
// 清除缓存
webView.clearCache(true);
在电脑上测试changeUsername
function setUsername()
{
var username=document.getElementById("username");
username.value="android";
}
在webView 中测试
用button调用javascript
TextView tv1=(TextView) findViewById(R.id.textView1);
tv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
webView.loadUrl("javascript:setUsername()");
} catch (Exception e) {
// TODO: handle exception
}
}
});
加call_phone权限
final class MyPhone
{
@JavascriptInterface
public void call(String mobile)
{
try {
Uri uri=Uri.parse("tel:"+mobile);//geo:latitude,longitude
Intent intent=new Intent(Intent.ACTION_CALL,uri);
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
}
}
}
MyPhone myPhone=new MyPhone();
webView.addJavascriptInterface(myPhone, "androidMyPhoneObject");
@SuppressLint("JavascriptInterface")
public class MainActivity extends Activity {
js代码
function callAndroid()
{
window.androidMyPhoneObject.call("138");
}
window.onload=function(){
var btn=document.getElementById("testjs");
btn.onclick=callAndroid;
}
Bootstrap响应式布局
Vue
androidApp
是个css框架
主要功能是实现了响应式布局。
案例:\bootstrap\WebContent\intel完整版
运行intel项目
是一个css前端框架,能制作响应式网页
Insert title here
Insert title here
test1
test2
test3
test4
Insert title here
div1
div2
超小屏幕 手机 0-768px |
小屏幕 平板768-992 |
中等屏幕 电脑992-1200 |
大屏幕 电脑1200-? |
Col-xs- |
Col-sm- |
Col-md |
Col-lg |
Bootstrap源码分析
分析
Col_xs超小
Col_sm小
Col_md 中
1111
222
333
4.Vue数据绑定
8.部署服务器端
4.部署数据库
-
文件—》新建查询编辑器
-
设置数据包的大小 ,如果不设置,sql文件态大,只能执行一部分sql
SET GLOBAL max_allowed_packet=1024*1024*400;
3.拷贝pd.sql中的所有内容到查询窗口中,选中所有内容,执行
4.刷新对象浏览器
5.看到pd_store数据库
5.部署代码
1.eclipse中import pdserver
2.Eclipse中打开server窗口,添加tomcat,
3.部署pdServer项目
4.把访问path设置成/
修改MySql密码
pdServer\src\main\resources\properties\ db.properties
5.通过http://ip:port 能看到商城电脑端的首页
6. /mobileIndex.html能看到商城移动端首页
9.创建响应式网页
在pdServer/src/main/webapp中创建网页pdIndex.html
Insert title here
10.Mounted事件
Insert title here
11.数据绑定
6.分析
Vue数据绑定
读取数据
//从vue框架取数据
foreach
设置数据
Mode.addAttribute(items)
New vue({
//把item放到vue框架中了
data:{items:[{title,price},{}]}
})
7.实现
{{item.title}}
{{item.price}}