Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2618076
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: Android平台

2013-06-26 00:22:13

    这是有米广告的文档:  参照这个就ok了;

    之前已经实践过Android和Servlet的通信了,所以就在原来代码上添加就ok了,最后要展示的效果如下:
                                     

布局文件:

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="wrap_content"
  5.     android:orientation="vertical" >

  6.     <!-- android:layout_weight="10" 失效了【最外层LinearLayout的布局高度是内容包裹】 -->
  7.     <LinearLayout
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content"
  10.         android:layout_marginBottom="15dp"
  11.         android:layout_marginTop="10dp"
  12.         android:layout_weight="10"
  13.         android:orientation="horizontal" >

  14.         <TextView
  15.             android:layout_width="wrap_content"
  16.             android:layout_height="wrap_content"
  17.             android:text="@string/user_name"
  18.             android:textSize="18sp" />

  19.         <EditText
  20.             android:id="@+id/user_name"
  21.             android:layout_width="fill_parent"
  22.             android:layout_height="wrap_content"
  23.             android:layout_marginLeft="5dp"
  24.             android:layout_marginRight="5dp"
  25.             android:background="@drawable/edit_text"
  26.             android:inputType="textPersonName"
  27.             android:padding="7dp"
  28.             android:text="" />
  29.     </LinearLayout>

  30.     <!-- android:layout_weight="2" 失效了 【最外层LinearLayout的布局高度是内容包裹】 -->
  31.     <LinearLayout
  32.         android:layout_width="fill_parent"
  33.         android:layout_height="wrap_content"
  34.         android:layout_marginBottom="15dp"
  35.         android:layout_weight="2"
  36.         android:orientation="horizontal" >

  37.         <TextView
  38.             android:layout_width="wrap_content"
  39.             android:layout_height="wrap_content"
  40.             android:text="@string/password"
  41.             android:textSize="18sp" />

  42.         <EditText
  43.             android:id="@+id/password"
  44.             android:layout_width="fill_parent"
  45.             android:layout_height="wrap_content"
  46.             android:layout_marginLeft="5dp"
  47.             android:layout_marginRight="5dp"
  48.             android:background="@drawable/edit_text"
  49.             android:inputType="textPassword"
  50.             android:padding="7dp"
  51.             android:text="" />
  52.     </LinearLayout>

  53.     <Button
  54.         android:id="@+id/login"
  55.         android:layout_width="fill_parent"
  56.         android:layout_height="wrap_content"
  57.         android:layout_marginLeft="59dp"
  58.         android:layout_marginRight="5dp"
  59.         android:background="@drawable/buton_style"
  60.         android:text="@string/login_btn" />
  61.     
  62.     <!-- 有米广告LinearLayout -->
  63.     <LinearLayout
  64.         android:id="@+id/adLayout"
  65.         android:layout_width="fill_parent"
  66.         android:layout_height="fill_parent"
  67.         android:layout_marginLeft="15dp"
  68.         android:layout_marginRight="5dp"
  69.         android:layout_marginTop="10dp"
  70.         android:orientation="vertical" />

  71. </LinearLayout>

AndroidManifest.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="com.android.taxi"
  4.     android:versionCode="1"
  5.     android:versionName="1.0" >

  6.     <uses-sdk
  7.         android:minSdkVersion="8"
  8.         android:targetSdkVersion="17" />

  9.     <!-- 有米广告授权 -->
  10.     <uses-permission android:name="android.permission.INTERNET" />
  11.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  12.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  13.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  14.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  15.     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
  16.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

  17.     <application
  18.         android:allowBackup="true"
  19.         android:icon="@drawable/ic_launcher"
  20.         android:label="@string/app_name"
  21.         android:theme="@style/AppTheme" >
  22.         <activity
  23.             android:name="com.android.taxi.LoginActivity"
  24.             android:label="@string/app_name" >
  25.             <intent-filter>
  26.                 <action android:name="android.intent.action.MAIN" />

  27.                 <category android:name="android.intent.category.LAUNCHER" />
  28.             </intent-filter>
  29.         </activity>
  30.         
  31.         <!-- 有米广告Activity-->
  32.         <activity
  33.             android:name="net.youmi.android.AdBrowser"
  34.             android:configChanges="keyboard|keyboardHidden|orientation"
  35.             android:theme="@android:style/Theme.NoTitleBar" >
  36.         </activity>
  37.         
  38.         <!-- 有米广告receiver-->
  39.         <receiver android:name="net.youmi.android.AdReceiver" >
  40.             <intent-filter>
  41.                 <action android:name="android.intent.action.PACKAGE_ADDED" />
  42.                 <action android:name="android.intent.action.PACKAGE_INSTALL" />
  43.                 <data android:scheme="package" />
  44.             </intent-filter>
  45.         </receiver>
  46.         
  47.         <!-- 有米广告service-->
  48.         <service android:name="net.youmi.android.AdService" >
  49.         </service>

  50.     </application>

  51. </manifest>

Activity文件:

点击(此处)折叠或打开

  1. package com.android.taxi;

  2. import java.util.HashMap;
  3. import java.util.Map;

  4. import net.youmi.android.AdManager;
  5. import net.youmi.android.banner.AdSize;
  6. import net.youmi.android.banner.AdView;
  7. import net.youmi.android.spot.SpotManager;

  8. import com.android.taxi.R;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.os.Looper;
  12. import android.os.Message;
  13. import android.annotation.SuppressLint;
  14. import android.app.Activity;
  15. import android.view.Menu;
  16. import android.view.View;
  17. import android.widget.Button;
  18. import android.widget.EditText;
  19. import android.widget.LinearLayout;
  20. import android.widget.Toast;

  21. @SuppressLint("HandlerLeak")
  22. public class LoginActivity extends Activity {

  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_login);
  27.         
  28.         findViews();
  29.         setListeners();

  30.         ///< 有米广告展示
  31.         AdManager.getInstance(LoginActivity.this).init("537ef88653a2993c", "b9e10bcfe994a9fb", true);        
  32.         adView = new AdView(LoginActivity.this, AdSize.SIZE_320x50);
  33.         adLayout.addView(adView);
  34.         SpotManager.getInstance(LoginActivity.this).loadSpotAds();
  35.         SpotManager.getInstance(LoginActivity.this).showSpotAds(this);
  36.     }
  37.     
  38.     public void findViews()
  39.     {
  40.         button_login = (Button)findViewById(R.id.login);
  41.         user_name = (EditText)findViewById(R.id.user_name);
  42.         password = (EditText)findViewById(R.id.password);
  43.         adLayout = (LinearLayout)findViewById(R.id.adLayout);
  44.     }
  45.     
  46.     public void setListeners()
  47.     {
  48.         button_login.setOnClickListener(loginTest);
  49.     }
  50.     
  51.     private Button.OnClickListener loginTest = new Button.OnClickListener()
  52.     {
  53.         @SuppressLint("NewApi")
  54.         @Override
  55.         public void onClick(View arg0) {
  56.             // TODO Auto-generated method stub
  57.             String userName = null;
  58.             String passWord = null;
  59.             
  60.             ///< 简单判断用户是否输入用户名,是否输入密码
  61.             if ((userName = user_name.getText().toString()).isEmpty())
  62.             {
  63.                 if (password.getText().toString().isEmpty())
  64.                 {
  65.                     Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
  66.                     return;
  67.                 }
  68.                 Toast.makeText(LoginActivity.this, "请输入用户名!", Toast.LENGTH_SHORT).show();
  69.                 return;
  70.             }
  71.             else if ((passWord = password.getText().toString()).isEmpty())
  72.             {
  73.                 Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
  74.                 return;
  75.             }
  76.             
  77.             loginHandle(userName, passWord);
  78.             
  79.             //            ///< 发送用户名和密码到服务器进行校验,并获得服务器返回值
  80.             //            Map params = new HashMap();
  81.             //            params.put("user_name", userName);
  82.             //            params.put("password", passWord);
  83.             //            String encode = "utf-8";
  84.             //            
  85.             //            //            Toast.makeText(LoginActivity.this, params.get("user_name"), Toast.LENGTH_SHORT).show();
  86.             //            //            Toast.makeText(LoginActivity.this, params.get("password"), Toast.LENGTH_SHORT).show();
  87.             //
  88.             //            int responseCode = HttpUtils.sendMessage(params, encode);
  89.             //            if (0 == responseCode)
  90.             //            {
  91.             //                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
  92.             //            }
  93.             //            else if (1 == responseCode)
  94.             //            {
  95.             //                Toast.makeText(LoginActivity.this, "用户名错误!", Toast.LENGTH_SHORT).show();
  96.             //            }
  97.             //            else if (2 == responseCode)
  98.             //            {
  99.             //                Toast.makeText(LoginActivity.this, "密码错误!", Toast.LENGTH_SHORT).show();
  100.             //            }
  101.             //            else
  102.             //            {
  103.             //                Toast.makeText(LoginActivity.this, "异常!", Toast.LENGTH_SHORT).show();
  104.             //            }
  105.                         
  106.             //            ///< 发送用户名和密码到服务器进行校验,并获得服务器返回值
  107.             //            // TODO
  108.             //            if (userName.equals("admin") && passWord.equals("123"))
  109.             //            {
  110.             //                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
  111.             //            }
  112.             //            else
  113.             //            {
  114.             //                Toast.makeText(LoginActivity.this, "请核对用户信息!", Toast.LENGTH_SHORT).show();
  115.             //            }
  116.         }

  117.     };
  118.     
  119.     public void loginHandle(final String userName, final String password)
  120.     {
  121.         new Thread(new Runnable()
  122.         {
  123.             @Override
  124.             public void run()
  125.             {
  126.                 ///< 发送用户名和密码到服务器进行校验,并获得服务器返回值
  127.                 Map<String, String> params = new HashMap<String, String>();
  128.                 params.put("user_name", userName);
  129.                 params.put("password", password);
  130.                 String encode = "utf-8";
  131.                 
  132.                 //            Toast.makeText(LoginActivity.this, params.get("user_name"), Toast.LENGTH_SHORT).show();
  133.                 //            Toast.makeText(LoginActivity.this, params.get("password"), Toast.LENGTH_SHORT).show();

  134.                 int responseCode = HttpUtils.sendMessage(params, encode);
  135.                 Looper.prepare();
  136.                 if (0 == responseCode)
  137.                 {
  138.                     Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
  139.                 }
  140.                 else if (1 == responseCode)
  141.                 {
  142.                     Toast.makeText(LoginActivity.this, "用户名错误!", Toast.LENGTH_SHORT).show();
  143.                 }
  144.                 else if (2 == responseCode)
  145.                 {
  146.                     Toast.makeText(LoginActivity.this, "密码错误!", Toast.LENGTH_SHORT).show();
  147.                 }
  148.                 else
  149.                 {
  150.                     Toast.makeText(LoginActivity.this, "异常!", Toast.LENGTH_SHORT).show();
  151.                 }
  152.                 Looper.loop();
  153.                 
  154.                 //执行完毕后给handler发送一个空消息
  155.                 handler.sendEmptyMessage(0);
  156.             }
  157.         }).start();
  158.     }

  159.     @Override
  160.     public boolean onCreateOptionsMenu(Menu menu) {
  161.         // Inflate the menu; this adds items to the action bar if it is present.
  162.         getMenuInflater().inflate(R.menu.login, menu);
  163.         return true;
  164.     }
  165.     
  166.     //定义Handler对象
  167.     private Handler handler =new Handler(){
  168.         @Override
  169.         //当有消息发送出来的时候就执行Handler的这个方法
  170.         public void handleMessage(Message msg){
  171.             super.handleMessage(msg);
  172.             // TODO 处理UI
  173.         }
  174.     };
  175.         
  176.     private Button button_login;
  177.     private EditText user_name;
  178.     private EditText password;
  179.     
  180.     LinearLayout adLayout;
  181.     AdView adView;
  182. }
    其它的就在这,和之前一样:http://blog.chinaunix.net/uid-25799257-id-3774047.html ,,,就这样吧,夜已经深了;等周末有空会深入下 service receiver activity ....等,虽然之前大体了解!!

    对了,之前有个button_style忘记说了,这是按钮的一个风格.xml文件:buton_style.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="">
  3.     <item android:state_pressed="true" android:drawable="@drawable/button_up" />
  4.     <item android:state_focused="true" android:drawable="@drawable/button_up" />
  5.     <item android:drawable="@drawable/button_on" />
  6. </selector>
    可以实现点击时显示其它背景图片;所以风格也可以作为背景图片,类似java的.css文件...确实不错!


阅读(6587) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~