1. 程式人生 > >Android實時監控專案第二篇:登陸介面的設計

Android實時監控專案第二篇:登陸介面的設計

在開始核心功能的實現之前,我們先從最簡單的部分開始,設計第一個Activity介面,它主要用來獲取使用者輸入的需要連線的PC端的IP地址。

      該Activity對應的XML佈局檔案很簡單,就不詳細解釋了,如下:

  1. <TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:orientation="vertical"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="wrap_content"
    >
  5. <TextView
  6.     android:layout_width="match_parent"
  7.     android:layout_height="wrap_content"
  8.     android:textSize="50sp"/>
  9. <TableRow
  10.     android:gravity="center_horizontal">
  11.     <TextView
  12.         android:layout_width="match_parent"
  13.         android:layout_height="wrap_content"
  14.         android:text
    ="@string/ip"
  15.         android:textSize="20sp"/>
  16.     <EditText
  17.         android:id="@+id/edit_ip"
  18.         android:layout_width="match_parent"
  19.         android:layout_height="wrap_content"
  20.         android:hint="@string/input_ip"
  21.         android:singleLine="true"
  22.         android:selectAllOnFocus="true"/>
  23. </TableRow>
  24. <TextView
  25.     android:layout_width="match_parent"
  26.     android:layout_height="wrap_content"
  27.     android:textSize="50sp"/>
  28. <TableRow
  29.     android:gravity="center_horizontal">
  30.     <Button
  31.         android:id="@+id/button_connect"
  32.         android:layout_width="match_parent"
  33.         android:layout_height="wrap_content"
  34.         android:text="@string/connect"/>
  35. </TableRow>
  36. </TableLayout>

      
      在對應的Activity類中,我用正則表示式來判定輸入的IP地址是否合法,如果不合法則點選“連線”按鈕後,提示輸入不合法,並清空輸入框,如果合法,則跳轉到下一個Activity,並將輸入的IP地址通過intnet傳入到下一個Activity。

  1. package zyb.org.camera;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.Toast;  
  11. publicclass LoginActivity extends Activity {  
  12.     private EditText inputIP = null;  
  13.     private Button connectButton = null;  
  14.     @Override
  15.     protectedvoid onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_login);  
  18.         inputIP = (EditText)findViewById(R.id.edit_ip);  
  19.         connectButton = (Button)findViewById(R.id.button_connect);  
  20.         connectButton.setOnClickListener(new OnClickListener() {  
  21.             @Override
  22.             publicvoid onClick(View v) {  
  23.                 //每次按下connectButton,檢查ip地址是否合法,不合法則要重新輸入,直到合法才跳轉到下個介面
  24.                 String ipAddress = inputIP.getText().toString().trim();  
  25.                 if(!isboolIp(ipAddress)){  
  26.                     Toast.makeText(LoginActivity.this"IP地址不合法,請重新輸入", Toast.LENGTH_SHORT).show();  
  27.                     inputIP.setText("");  
  28.                 }else{  
  29.                     Intent intent = new Intent(LoginActivity.this, CameraActivity.class);  
  30.                     //將輸入的ip地址資料傳給CameraActivity
  31.                     intent.putExtra("ipname", ipAddress);  
  32.                     startActivity(intent);  
  33.                 }  
  34.             }  
  35.         });  
  36.     }  
  37.     /** 
  38.      * 判斷是否為合法IP 
  39.      * 這裡的匹配結果只要符合x.x.x.x即可,x必須為1至3位數, 
  40.      * 但沒有限制到255或524以下,因此不算是完美的匹配法,但 
  41.      * 已經可以遮蔽掉常見的不合法輸入,因此也夠用了 
  42.      */
  43.     publicboolean isboolIp(String ipAddress)  
  44.     {  
  45.         String ip = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";   
  46.         return ipAddress.matches(ip);   
  47.     }  
  48.     @Override
  49.     publicboolean onCreateOptionsMenu(Menu menu) {  
  50.         // Inflate the menu; this adds items to the action bar if it is present.
  51.         getMenuInflater().inflate(R.menu.activity_login, menu);  
  52.         returntrue;  
  53.     }  
  54. }  

    下篇將講述通過攝像頭實時預覽視訊相關操作的實現