教我徒弟Android開發入門(一)


前言:

這個系列的教程是為我徒弟准備的,也適合還不懂java但是想學android開發的小白們~

本系列是在Android Studio的環境下運行,默認大家的開發環境都是配置好了的

沒有配置好的同學請前去百度下載Android Studio

之后,參照我的下面這個教程將Android Studio配置好

http://www.cnblogs.com/kexing/p/7455786.html

 

主要目標:

第一篇就簡單從界面開始吧,今天我們的目標就是做一個仿QQ登錄界面的登錄界面,輸入相應的賬號與密碼,點擊登錄按鈕,提示登錄成功,若輸入不對的賬號與密碼,則提示登錄失敗

 

 

 

 

正文:

新建一個項目

 file->New project

接下來就會出現新建項目的相關設置

下一步

 

 下一步

 

 下一步

 

 

 點擊finish就成功創建一個APP了

 

之后在左側的目錄依次展開,找到activity_mian的這個文件,打開

 

可能你的界面沒有出現如我圖中右側的預覽圖,請查看右側,有個priview,點擊它就能顯示預覽圖了

 

這個預覽圖也就是我們打開APP顯示的界面預覽圖

我們要仿QQ界面登錄的界面,也就是要修改這個界面所要顯示的東西,這里需要Android的View基礎部分,我在這里不多說,建議大家先去了解一番再往下看

 

我們切換到Design的模式,在這個模式中,把所需要的控件拖到相應位置,之后再設置相關屬性大小即可

 

 雖然在Design模式的右邊,有設置屬性的相關選項,不過我還是喜歡直接回到之前的界面,也就是Text模式中進行代碼修改

 

 貼上代碼

 

activity_main

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.wan.login.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="30dp" android:gravity="center" android:text="用戶名" android:textSize="15dp" android:layout_marginLeft="62dp" android:layout_marginStart="62dp" android:layout_marginTop="114dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="30dp" android:gravity="center" android:text="密碼" android:layout_alignEnd="@+id/textView" android:layout_alignRight="@+id/textView" android:layout_below="@+id/textView" android:layout_marginTop="40dp" android:layout_alignLeft="@+id/textView" android:layout_alignStart="@+id/textView"/> <EditText android:id="@+id/editText3" android:layout_width="200dp" android:layout_height="wrap_content" android:ems="10" android:gravity="center" android:textSize="15dp" android:inputType="textPersonName" android:layout_alignTop="@+id/textView" android:layout_toRightOf="@+id/textView" android:layout_toEndOf="@+id/textView" android:layout_marginLeft="30dp" android:layout_marginStart="30dp"/> <EditText android:id="@+id/editText5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText3" android:layout_alignStart="@+id/editText3" android:ems="10" android:inputType="textPassword" android:layout_alignBottom="@+id/textView1"/> <Button android:id="@+id/button" android:layout_width="150dp" android:layout_height="wrap_content" android:text="登錄" android:textColor="#FCFCFC" android:background="#1C86EE" android:layout_marginTop="28dp" android:layout_below="@+id/textView1" android:layout_alignLeft="@+id/textView1" android:layout_alignStart="@+id/textView1" android:layout_alignRight="@+id/editText5" android:layout_alignEnd="@+id/editText5"/> </RelativeLayout>

不要全部都復制哦,肯定會錯誤的

還有MainActivity

package com.wan.login; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity implements View.OnClickListener { /** * 登錄 */ private Button mButton; private EditText mname,mpassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mname = (EditText) findViewById(R.id.editText3); mpassword = (EditText) findViewById(R.id.editText5); mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: String name,passwoed; String user = "User"; String userpassword = "123456"; name = mname.getText().toString(); passwoed = mpassword.getText().toString(); final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); if(name.equals(user)&&passwoed.equals(userpassword)){ builder.setMessage("登錄成功"); builder.setNegativeButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); }else{ builder.setMessage("登錄失敗"); builder.setNegativeButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); } builder.show(); break; default: break; } } }

 

之后使用模擬器就是可以看到結果了

 

 

如果想要安裝到手機的話,請看下面

 

成功生成apk之后,在右下角會有個提示框,點擊show,就是進入到資源管理器

 

教我徒弟Android開發入門(二)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM