Android實時監控專案第二篇:登陸介面的設計
阿新 • • 發佈:2019-01-01
在開始核心功能的實現之前,我們先從最簡單的部分開始,設計第一個Activity介面,它主要用來獲取使用者輸入的需要連線的PC端的IP地址。
該Activity對應的XML佈局檔案很簡單,就不詳細解釋了,如下:
- <TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
-
android:layout_height="wrap_content"
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="50sp"/>
- <TableRow
- android:gravity="center_horizontal">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
-
android:text
- android:textSize="20sp"/>
- <EditText
- android:id="@+id/edit_ip"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/input_ip"
- android:singleLine="true"
-
android:selectAllOnFocus="true"/>
- </TableRow>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="50sp"/>
- <TableRow
- android:gravity="center_horizontal">
- <Button
- android:id="@+id/button_connect"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/connect"/>
- </TableRow>
- </TableLayout>
在對應的Activity類中,我用正則表示式來判定輸入的IP地址是否合法,如果不合法則點選“連線”按鈕後,提示輸入不合法,並清空輸入框,如果合法,則跳轉到下一個Activity,並將輸入的IP地址通過intnet傳入到下一個Activity。
- package zyb.org.camera;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- publicclass LoginActivity extends Activity {
- private EditText inputIP = null;
- private Button connectButton = null;
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- inputIP = (EditText)findViewById(R.id.edit_ip);
- connectButton = (Button)findViewById(R.id.button_connect);
- connectButton.setOnClickListener(new OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- //每次按下connectButton,檢查ip地址是否合法,不合法則要重新輸入,直到合法才跳轉到下個介面
- String ipAddress = inputIP.getText().toString().trim();
- if(!isboolIp(ipAddress)){
- Toast.makeText(LoginActivity.this, "IP地址不合法,請重新輸入", Toast.LENGTH_SHORT).show();
- inputIP.setText("");
- }else{
- Intent intent = new Intent(LoginActivity.this, CameraActivity.class);
- //將輸入的ip地址資料傳給CameraActivity
- intent.putExtra("ipname", ipAddress);
- startActivity(intent);
- }
- }
- });
- }
- /**
- * 判斷是否為合法IP
- * 這裡的匹配結果只要符合x.x.x.x即可,x必須為1至3位數,
- * 但沒有限制到255或524以下,因此不算是完美的匹配法,但
- * 已經可以遮蔽掉常見的不合法輸入,因此也夠用了
- */
- publicboolean isboolIp(String ipAddress)
- {
- String ip = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
- return ipAddress.matches(ip);
- }
- @Override
- publicboolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_login, menu);
- returntrue;
- }
- }
下篇將講述通過攝像頭實時預覽幀視訊相關操作的實現