日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

AndroidStudio使用入门

發布時間:2025/3/11 Android 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AndroidStudio使用入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AndroidStudio使用入門

  • 1_AndroidStudio activity的基本使用
    • 1.1_MainActivity和activity_main的初識
    • 1.2_Activity的清單文件簡介
    • 1.3_幾種重要文件的介紹
    • 1.4_基本布局的認識與使用
      • 1.4.1_RelativeLayout(相對布局)
      • 1.4.2_線性布局
  • 2_訪問資源的方式
    • 2.1_java訪問資源的方式
    • 2.2_xml訪問資源的方式
  • 3_控件的使用
    • 3.1_通用屬性
    • 3.2_選擇器的使用
    • 3.3_TextView的使用
    • 3.4_Button按鈕和點擊事件的添加
    • 3.5_EditText的使用
    • 3.6_單選框radio button和radio group的使用
    • 3.7_CheckBox的使用
    • 3.8_Toast提示的使用
    • 3.9_AlertDialog的使用
      • 3.9.1_AlertDialog的簡單使用
      • 3.9.2_AlertDialog的單選框使用
      • 3.9.3_AlertDialog的自定義界面的使用
    • 3.10_ListView的使用
      • 3.10.1_SimpleAdapter的使用(略)
      • 3.10.2_ArrayAdapter的使用
      • 3.10.3_BaseAdapter的使用(略)
    • 3.11_ContextMenu長按事件
    • 3.12_頁面分欄LayoutInflater
  • 4_生命周期與intent的使用
    • 4.1_生命周期
    • 4.2_頁面跳轉
    • 4.3_intent跳轉頁面傳值
    • 4.4_intent傳值自定義類
    • 4.5_回傳數據
  • 5_Server服務的使用
  • 6_小問題、小技巧匯總
    • 6.1_去除AppCompatActivity自帶標題
    • 6.2_解決不能安裝問題
    • 6.3_Toast在新線程中不能正常使用
    • 6.4_按鈕透明
    • 6.5_添加gif圖片
    • 6.6_自動導包

1_AndroidStudio activity的基本使用

log.v("","");//在控制面板輸出內容

1.1_MainActivity和activity_main的初識

  • 在MainActivity所在文件夾中可以創建任意“?Activity”文件作為后端邏輯代碼文件
  • 同理在activity_main所在文件夾中可以創建任意“activity_?”文件作為前端頁面控制代碼文件
    注意兩個文件名的“?”部分要相互對應
  • 1.2_Activity的清單文件簡介

    1.3_幾種重要文件的介紹

  • Android中的界面是由兩個文件組成的 一個是Activity的Java文件 還有一個是xml的布局文件.Java文件都放在java的文件下布局文件全部在res/layout下

  • layout下的文件都是xml文件

  • res下的文件不允許有大寫字母

  • manifests文件夾下,AndroidManifest.xml:清單文件。可以配置應用的第一個啟動的Activity,注冊Activity,修改應用圖標及應用名稱

  • values自定義變量名
    colors.xml:

    strings.xml:
    在此文件中定義變量類似C語言中的#define
    多語言支持方式

  • drawable文件夾和mipmap文件夾
    一般將APP的icon放在minmap文件夾下,其他圖片資源放在drawable文件夾下

  • 1.4_基本布局的認識與使用

    基本布局的認識

  • View:表示為Android視圖
    Java文件和xml文件都能編寫View
    不管是Java還是xml文件最后都會變成Java文件的視圖(其中java編寫的視圖和java課上學的視圖一樣,一般開發軟件不用java編寫視圖

  • ViewGroup:表示為布局(在xml中布局可以嵌套)
    布局控件可以放其它控件常用布局 LinearLayout(線性布局)、RelativeLayout(相對布局)、ConstraintLayout(約束布局)
    如果只有一個子控件,則不需要寫orientation屬性,有多個必須要寫,在新工具不一定
    常用控件 Button(按鈕)、TextView(文本)、EditText(輸入框)

  • 根布局需要加代碼<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"
    其中xmlns:app只在相對布局中加
  • 1.4.1_RelativeLayout(相對布局)

    沒有方向性,但是有Z軸,代碼在后的Z軸值越大,即可以懸浮必須要參照物
    1、布局本身為參照
    android:layout_centerHrizontal 水平居中
    android:layout_centerVertical 垂直居中
    android:layout_centerInparent 相對于父元素完全居中
    android:layout_alignParentBottom 貼緊父元素的下邊緣
    android:layout_alignParentLeft 貼緊父元素的左邊緣
    android:layout_alignParentRight 貼緊父元素的右邊緣
    android:layout_alignParentTop 貼緊父元素的上邊緣

    2、通過id來指定參照物
    android:layout_below 在某元素的下方
    android:layout_above 在某元素的的上方
    android:layout_toLeftOf 在某元素的左邊
    android:layout_toRightOf 在某元素的右邊
    android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊
    android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊
    android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊
    android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊
    3. 屬性值為具體的像素值,如 30dip,40px
    android:layout_marginBottom 離某元素底邊緣的距離
    android:layout_marginLeft 離某元素左邊緣的距離
    android:layout_marginRight 離某元素右邊緣的距離
    android:layout_marginTop 離某元素上邊緣的距離

    1.4.2_線性布局

    如果只有一個子控件,則不需要寫orientation屬性,有多個必須要寫,在新工具不一定在線性布局中
    空間大小:android:layout_width="" android:layout_height=""
    match_parent只會占滿剩余的空間 wrap_content適應文本大小 除此之外,還可以寫固定數值,單位是dp或dip
    layout_weight是權重

    <LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><Buttonandroid:id="@+id/button_alterdialog"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="刪除"android:onClick="delete"/><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="增加"android:onClick="delete"/></LinearLayout>

    2_訪問資源的方式

    2.1_java訪問資源的方式

    R.resource_type.name//R可以理解為資源中的最高包 Android.R.//官方資源

    2.2_xml訪問資源的方式

    @pakage_name:resource_type/name

    3_控件的使用

    3.1_通用屬性

  • gravity(對齊方式):center、left、rightbottom
    多個布局方式以"|"分隔

  • 邊距
    內邊距:padding
    外邊距:margin

  • 限制文本行數
    android: lines="2" android:ellipsize="end"----超出的部分“。。。”表示

  • 放置圖片(圖片存儲在mipmap(drawable)文件夾)
    Android:drawable
  • 背景圖片和顏色
    Android:background="@mipmap/drawable_name"(背景圖片)
    Android:background=“顏色”
  • 3.2_選擇器的使用

    3.3_TextView的使用

    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="16sp"android:textColor="#ffffff" android:padding="10dip" android:background="#cc0000" android:layout_gravity="center" android:text="Android Studio 工具箱"/>

    3.4_Button按鈕和點擊事件的添加

  • Button繼承于TextView

  • android背景選擇器的使用

  • Button點擊事件的添加(最簡單的方式,另外兩種方式見3.6

  • java文件下:
    MainActivity文件中

    public void login(View view){//動作}

    res.layout文件夾下activity_main文件中

    <Buttonandroid:layout_width="90dp"android:layout_height="70dp"android:text="登錄"android:textSize="25dp"android:onClick="login"></Button>

    3.5_EditText的使用

    基本使用

    <EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="提示"android:inputType="datetime"></EditText>

    其中inputType是輸入類型,輸入類型有密碼、數字、文本、日期等

    如何在后端獲取編輯框中的內容

    在java文件中

    EditText edit_text1 = findViewById(R.id.id名);edit_text1.getText().toString;

    3.6_單選框radio button和radio group的使用

  • 直接應用:
  • RadioGroup radioGroup = findViewById(R.id.radiogroup);radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId){case R.id.radiobutton_man:{Log.i("radiobutton_man","男生");break;}case R.id.radiobutton_woman:{Log.i("radiobutton_woman","女生");break;}}}});

    效果:

    2. 繼承接口:

    public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);RadioGroup radioGroup = findViewById(R.id.radiogroup_1);radioGroup.setOnCheckedChangeListener(this);//this在此使用的多態的屬性,表示繼承的接口類而不是MainActivity類}@Override//實現接口中的抽象類public void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId){case R.id.radiobutton_man:{Log.i("radiobutton_man","男生");break;}case R.id.radiobutton_woman:{Log.i("radiobutton_woman","女生");break;}}} }

    效果:

    3.7_CheckBox的使用

    <CheckBoxandroid:id="@+id/chaoxiong"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="超兇"></CheckBox> CheckBox checkBox = findViewById(R.id.chaoxiong);//獲取控件checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {//進行監聽@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){//如果被選中Log.i("OnCheckedChange",buttonView.toString()+"被選中");}}});

    3.8_Toast提示的使用

    Toast.makeText(MainActivity.this,"被選中",Toast.LENGTH_LONG).show();

    3.9_AlertDialog的使用

    3.9.1_AlertDialog的簡單使用

    <Buttonandroid:id="@+id/button_alterdialog"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="刪除"android:onClick="delete"/> public void delete(View v){AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);builder.setTitle("確認").setIcon(R.mipmap.why).setMessage("確定刪除嗎?").setPositiveButton("確認", new DialogInterface.OnClickListener() {//創建確認選項按鈕及其點擊事件@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this,"刪除成功",Toast.LENGTH_SHORT).show();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {//創建取消選項按鈕及其點擊事件@Overridepublic void onClick(DialogInterface dialog, int which) {}});//下面兩個千萬不能忘記AlertDialog dialog = builder.create();//創建dialog.show();//顯示}

    效果:

    3.9.2_AlertDialog的單選框使用

    <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="選擇小周周的午餐"android:onClick="select"></Button> public void select(View view){AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);builder.setTitle("饞的流口水了都").setIcon(R.mipmap.greedy).setSingleChoiceItems(new String[]{"奧爾良雞腿", "菠蘿", "冰激凌"}, 0, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "中午吃"+which, Toast.LENGTH_SHORT).show();}});//下面兩個千萬不能忘記AlertDialog dialog = builder.create();//創建dialog.show();//顯示}

    效果:

    3.9.3_AlertDialog的自定義界面的使用

    <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="打開自定義界面"android:onClick="customView"></Button> public void customView(View view){AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);builder.setTitle("新窗口").setView(R.layout.activity_new);//下面兩個千萬不能忘記AlertDialog dialog = builder.create();//創建dialog.show();//顯示};

    效果:

    3.10_ListView的使用

    3.10.1_SimpleAdapter的使用(略)

    3.10.2_ArrayAdapter的使用

    <ListViewandroid:id="@+id/listview"android:layout_width="wrap_content"android:layout_height="match_parent"></ListView> ListView listView = findViewById(R.id.listview);String[] name = {"菠蘿","青皮桔","香蕉","西瓜"};ArrayAdapter arrayAdapter = new ArrayAdapter(newActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1,name);listView.setAdapter(arrayAdapter);

    3.10.3_BaseAdapter的使用(略)

    3.11_ContextMenu長按事件


    詳解請轉

    3.12_頁面分欄LayoutInflater


    代碼詳情

    4_生命周期與intent的使用

    4.1_生命周期

    @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);LogUtils.d(TAG,"onCreate ---> 創建時調用"); }@Override protected void onRestart() {super.onRestart();LogUtils.d(TAG,"onRestart ---> 重啟時調用"); }@Override protected void onStart() {super.onStart();LogUtils.d(TAG,"onStart ---> 即將可見不可交互時調用"); }@Override protected void onResume() {super.onResume();LogUtils.d(TAG,"onResume ---> 可見可交互時調用"); }@Override protected void onPause() {super.onPause();LogUtils.d(TAG,"onPause ---> 即將暫停時調用"); }@Override protected void onStop() {super.onStop();LogUtils.d(TAG,"onStop ---> 即將停止不可見時調用"); }@Override protected void onDestroy() {super.onDestroy();LogUtils.d(TAG,"onDestroy ---> 即將銷毀時調用"); }

    4.2_頁面跳轉

    AndroidManifest.xml代碼注意,若要顯示某個頁面,必須將其在此文件夾中聲明

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.myapplication"><applicationandroid:allowBackup="true"android:icon="@mipmap/angry"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".newActivity"></activity></application></manifest> public void windowSkip(View view){//按鈕點擊事件Intent intent = new Intent();intent.setClass(MainActivity.this,newActivity.class);//從前者跳往后者startActivity(intent);//啟動}

    4.3_intent跳轉頁面傳值

    MainActivity文件:

    public void loginSkip(View view){Intent intent = new Intent();intent.setClass(MainActivity.this,LoginActivity.class);//從前者跳往后者intent.putExtra("name","jiajia");intent.putExtra("sex","男");intent.putExtra("hobby",new String[] {"籃球","乒乓球","羽毛球"});startActivity(intent);//啟動} }

    LoginActivity文件:

    public class LoginActivity extends AppCompatActivity {protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);//接收傳值Intent intent = getIntent();String name = intent.getStringExtra("name");String sex = intent.getStringExtra("sex");String[] hobbies = intent.getStringArrayExtra("hobby");Log.i("LoginActivity",name+sex+hobbies);//打印} }

    4.4_intent傳值自定義類

    只要繼承了parcelable或者serializable接口就可以進行傳值,注意接收的時候需要強行轉成此自定義類的類型

    4.5_回傳數據

    回傳數據

    與無回傳數據類似,不同之處在于:

  • 啟動跳轉頁面的函數需要加上第二個int參數用于標識跳轉的頁面
  • startActivityForResult(intent, nubmber);
  • 當關閉跳轉頁面的時候需要返回兩個參數(一個是判斷是否有返回數據的>int類型,一般RESULT_OK=1,RESULT_CANCELED=0,一個是data)
  • String uriString = editText.getText().toString();//獲取數據Uri data = Uri.parse(uriString)//轉換數據類型//設置結果其中Intent()第二個參數類型是Uri//Intent result = new Intent(null, data);setResult(RESULT_OK, result);//設置返回結果,當此頁面結束時返回
  • 原頁面接收數據
  • //重寫父類Activity的函數onActivityResult protected void onActivityResult(int requestCode, int resultCode, Intent data){ //requestCode是啟動跳轉頁面時賦予頁面的int類型的number標識符 startActivityForResult(intent, nubmber); //resultCode是跳轉頁面返回數據結果時設置的判斷是否有結果回傳的int參數 setResult(RESULT_OK, result); super.onActivityResult(requestCode, resultCode, data);switch(requestCode){case number1://如果是頁面number1if (resultCode == RESULT_OK){//判斷是否回傳了數據Uri uriData = data.getData();//獲取數據}break;case SUBACTIVITY2:break;} }

    5_Server服務的使用

    public class SimpleRandomServiceDemo extends AppCompatActivity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_simple_random_service_demo);Button startButton = (Button)findViewById(R.id.start);Button stopButton = (Button)findViewById(R.id.stop);final Intent serviceIntent = new Intent(this, RandomService.class);startButton.setOnClickListener(new Button.OnClickListener(){public void onClick(View view){startService(serviceIntent);}});stopButton.setOnClickListener(new Button.OnClickListener(){public void onClick(View view){stopService(serviceIntent);}});} } public class RandomService extends Service {@Overridepublic void onCreate() {super.onCreate();Toast.makeText(this, "(1) 調用onCreate()",Toast.LENGTH_LONG).show();}@Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);Toast.makeText(this, "(2) 調用onStart()",Toast.LENGTH_SHORT).show();double randomDouble = Math.random();String msg = "隨機數:"+ String.valueOf(randomDouble);Toast.makeText(RandomService.this,msg, Toast.LENGTH_SHORT).show();}@Overridepublic void onDestroy() {super.onDestroy();Toast.makeText(this, "(3) 調用onDestroy()",Toast.LENGTH_SHORT).show();}@Overridepublic IBinder onBind(Intent intent) {return null;} } <application <service android:name=".first.RandomService"></service> </application>

    6_小問題、小技巧匯總

    6.1_去除AppCompatActivity自帶標題


    進入AndroidManifest.xml文件

    ctrl進入AppTheme改代碼如下

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    6.2_解決不能安裝問題

    Android studio 打包apk后發給別人,安裝失敗。

    失敗原因顯示:應用是非正式發布版本,當前設備不支持安裝。

    在自己手機上也無法安裝,之后連接USB進行調試時可以運行,打包成APK安裝就會顯示下圖錯誤:

    2.原因分析:
    造成該問題的原因是Android Studio 3.0會在debug apk的manifest文件application標簽里自動添加 android:testOnly="true"屬性。該屬性導致在IDE中使用Run生成的apk在大部分手機上只能用adb install -t 來安裝。

    這種apk在某些手機上甚至安裝不了。

    3.解決方法:
    在gradle.properties 文件中添加如下指令:

    android.injected.testOnly=false

    6.3_Toast在新線程中不能正常使用

    由于主線程會自動初始化loop,所以在主線程不會報錯

    //在所在線程加 Looper.prepare();//對loop進行初始化,不然toast報異常 Toast.makeText(LoginActivity.this,"登錄成功",Toast.LENGTH_SHORT).show(); Looper.loop();//對loop進行初始化,不然toast報異常

    6.4_按鈕透明

    兩大屬性介紹:

    android:alpha="0" //實現按鈕透明化,按鈕就不可見了,包括上面的文字 android:background="#00000000"//設置按鈕背景,背景透明,但是按鈕上的文字仍然可見。也可以把圖片設置為背景

    6.5_添加gif圖片

    轉自

    6.6_自動導包

    自動導包方法import

    總結

    以上是生活随笔為你收集整理的AndroidStudio使用入门的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。