Android开发入门
build.gradle
plugins { id 'com.android.application' }
android { compileSdkVersion 35 buildToolsVersion "35.0.0"
defaultConfig { applicationId "com.example.demo" minSdkVersion 16 targetSdkVersion 35 versionCode 1 versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { cppFlags '-std=c++11' } } }
buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } externalNativeBuild { cmake { path file('src/main/cpp/CMakeLists.txt') version '3.10.2' } } buildFeatures { viewBinding true } }
dependencies { implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.2.1' implementation 'androidx.constraintlayout:constraintlayout:2.0.1' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }
|
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Demo"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest>
|
安卓程序执行路口
如果app带有application的话,执行入口就在application中的android:name所指向的类中,一般的app里面都没有application,加固的app中一般都有application
application指向的类程序员能控制的最早的类
基本控件的使用
使用android:onClick 进行绑定点击事件
<Button android:id="@+id/button" android:layout_width="283dp" android:layout_height="71dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.345" android:onClick="test" />
public void test(View view) { Log.d("xiaojianbang","test test"); }
|
使用id进行绑定
<Button android:id="@+id/button" android:layout_width="283dp" android:layout_height="71dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.345" /> Button button = binding.button; button.setOnClickListener(new View.onCLickListener( @Override public void onCick(View view){ Lod.d("xiaojianbang","onClick"); } ));
|
使用多态的方式进行绑定
<Button android:id="@+id/button" android:layout_width="283dp" android:layout_height="71dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.345" />
Button button = binding.button; button.newOnClickListener(new MyClick());
class MyClick implements View.OnClickListener{ @Override public void onClick(View view){ Log.d("xiaojianbang",onClickMyClick); } }
|
直接让MainActivity实现接口
有多种按钮时采用这个方法
public class MainActivity extends AppCompatActivity implements
button.setOnClickListerner(this);
有多个按钮时,采用view.getId()进行区分
|
Toast
@Overrid public void onClick(View view){ switch(view.getId()){ case R.id.button: Log.d("xiaojianbang","onClick"); Toast.makeText(Main.this,"xiaojianbang",Toast.LENGTH_LONG).show(); } }
|
TextView
TextView textView = binding.textVeiw; @Override vodi onClick(View view){ switch(view.getId()){ case R.id.button: Log.d("xiaojianbang",MainOnClick); Toast.makeText(MainActivity.this,"xiaojianbang",Toast.LENGTH_LONG).show(); textView.setText("lelelele"); break; } }
|
EditText
@Override public void onCick(View view){ switch(view.getId()){ case R.id.button(): Log.d("xiaojianbang","onClck"); Toast.makeText(MainActivity.this,"xiaojianbang",Toast.LENGTH_LONG).show(); textView.setText("lele"); String username = editText.getText().toString().trim(); if(TextUtile.isEmpty(username)){ Log.d("xiaojiambang","username is null"); }else{ Log.d("xiaojianbang",username); } break; }
}
|