TextView
基础属性详解
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tv_one" android:textColor="#CDDC39" android:textStyle="normal" android:textSize="30sp" android:background="#2196F3" android:gravity="center" android:text="lele" /> </LinearLayout>
|
string这种使用方式不是规范的,规范的使用方式为,在app/src/main/res/values/strings.xml下添加
<resources> <string name="app_name">AndroidBasis</string> <string name="tv_one">lele</string> </resources>
|
再以这样的方式使用
android:text="@string/tv_one"
|
color有8位数字组成,前两位代表透明度,后六位分别代表红绿蓝的程度
带阴影的TextView
为了使效果明显去掉背景颜色
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_one" android:layout_width="444dp" android:layout_height="848dp" android:gravity="center" android:shadowColor="#2196F3" android:shadowRadius="3.0" android:shadowDx="10.0" android:shadowDy="10.0" android:text="@string/tv_one" android:textColor="@color/white" android:textSize="30sp" android:textStyle="normal" /> </LinearLayout>
|
实现跑马灯效果的TextView
基本设置
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<TextView android:id="@+id/tv_one" android:layout_width="444dp" android:layout_height="848dp" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:gravity="center" android:marqueeRepeatLimit="marquee_forever" android:shadowColor="#2196F3" android:shadowDx="10.0" android:shadowDy="10.0" android:shadowRadius="3.0" android:singleLine="true" android:text="@string/tv_one" android:textColor="@color/white" android:textSize="30sp" android:textStyle="normal"/> </LinearLayout>
|
三种方式使TextView获取焦点
第一种:点击获得焦点
添加 android:clickable="true"
|
第二种:自定义一个类,调用isFocused()
自定义一个类,调用isFocused() MyTextView: package a.b.c.mytextview;
import android.content.Context; import android.util.AttributeSet; import android.widget.TextView;
import androidx.annotation.Nullable;
public class MyTextView extends TextView { public MyTextView(Context context) { super(context); }
public MyTextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); }
public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); }
@Override public boolean isFocused() { return true; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<a.b.c.mytextview.MyTextView android:id="@+id/tv_one" android:layout_width="444dp" android:layout_height="848dp" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:shadowColor="#2196F3" android:shadowDx="10.0" android:shadowDy="10.0" android:shadowRadius="3.0" android:singleLine="true" android:text="@string/tv_one" android:textColor="@color/white" android:textSize="30sp" android:textStyle="normal"/> </LinearLayout>
|
第三种:添加标签
StateListDrawable
改变背景颜色直接使用 android:background=”@color/black”是没有效果的,需要修改themes.xml文件
更换背景图片
导入图片
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical">
<Button android:text="我是按钮" android:background="@drawable/btn_selector" android:layout_width="200dp" android:layout_height="100dp"/> </LinearLayout>
|
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/baseline_10k_24" android:state_pressed="true"/> <item android:drawable="@drawable/baseline_arrow_circle_down_24"/> </selector>
|
更换背景图片颜色
创建color文件
创建ColorResourec File
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical">
<Button android:layout_width="200dp" android:layout_height="100dp" android:background="@drawable/btn_selector" android:backgroundTint="@color/btn_color_selector" android:text="我是按钮" /> </LinearLayout>
|
点击事件、长按事件、触摸事件
package a.b.c.mybutton;
import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.Button;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity { private static final String TAG = "lele";
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); Button btn = findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { ;
@Override public void onClick(View view) { Log.e(TAG, "onClick: ");
} }); btn.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { Log.e(TAG, "onLongClick: "); return false; } }); btn.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { Log.e(TAG, "onTouch: "+ motionEvent.getAction() ); return false; } }); } }
|
若是将OnTouch的返回值改为true,另外两个都不会执行,称为事件被OnTouch消费了;若是将OnLongClick改为true,OnTouch会执行,OnClick不会执行
另一种代替OnClick的方法
android:onClick="leleOnclick" 在MainActivity中创造此方法 public void leleOnclick(View view) {
}
|
EditText
package a.b.c.myedittext;
import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity { private EditText edt_test;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); Button btn = findViewById(R.id.btn_get); edt_test = findViewById(R.id.edt); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String text = edt_test.getText().toString(); Log.e("lele", "输入的内容 "+text ); } }); } }
|
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> <EditText android:layout_width="wrap_content" android:layout_height="100dp" android:hint="请输入用户名" android:textColorHint="#03A9F4" android:inputType="phone" android:background="@color/white" android:id="@+id/edt" />
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher_background" android:inputType="textPassword" android:hint="请输入用户名" android:drawablePadding="30dp" android:paddingLeft="20dp" android:background="@color/white" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取内容" android:id="@+id/btn_get" />
</LinearLayout>
|
ImageView
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical">
<ImageView android:layout_width="200dp" android:layout_height="200dp" android:scaleType="fitXY" android:src="@drawable/ic_launcher_foreground" android:maxHeight="200dp" android:maxWidth="200dp" android:adjustViewBounds="true" />
</LinearLayout>
|
ProgressBar
package a.b.c.myprogressbar;
import android.os.Bundle; import android.view.View; import android.widget.ProgressBar;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity { private ProgressBar progressBar; private ProgressBar progressBar2;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); progressBar = findViewById(R.id.pb); progressBar2 = findViewById(R.id.pb2);
}
public void leleClick(View view) { if(progressBar.getVisibility() == View.GONE){ progressBar.setVisibility(View.VISIBLE); }else{ progressBar.setVisibility(View.GONE); } }
public void load(View view) { int progress = progressBar2.getProgress(); progress+=10; progressBar2.setProgress(progress); } }
|
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pb"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:text="显示或隐藏进度条" android:onClick="leleClick" />
<ProgressBar style="?android:attr/progressBarStyleHorizontal" android:max="100" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pb2" android:onClick="lele" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn2" android:text="模拟下载" android:onClick="load"/>
</LinearLayout>
|
Notification与NotificationManager
简单来说就是通知的小图标不能带颜色,标题、文本内容、小图标是必须要设置的
MainActivity
package a.b.c.mynotification;
import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.view.View;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.NotificationCompat; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity { private NotificationManager manager; private Notivication notification; @Override protected void onCreate(Bundle saveInstanceState){ super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); if(Build.VERSION.SDK_INT >= Build.VERSION_CODE.0){ NotificationChanner channer = new NotificationChanner("lele","测试通知",NotificationManager.IMPORTANCE_HIGH); manager.createNofificationChanner(channel); } Intent intent = new Intent(this, NotificationActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, intent, PendingIntent.FLAG_IMMUTABLE
); notification = new NotificationCompat.Builder(this,"lele") .setContentTitle("官方通知") .setContentText("人生若只如初见,何事秋风悲画扇") .setSmallIcon(R.drabable.baseline_android_24) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ceshi1)) .setColor(Color.parseCOlor("#ff0000")) .setContentIntent(pendingIntent) .setAutoCancel(true) .build(); public void sendNotification(View view){ manager.notify(1,notification); } public void cacelNotification(View view){ manager.cancel(1); } } }
|
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" >
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送通知" android:onClick="sendNotification" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="cacelNotification" android:text="取消通知" />
</LinearLayout>
|
NotificationActivity
package a.b.c.mynotification;
import android.app.Activity; import android.os.Bundle; import android.util.Log;
import androidx.annotation.Nullable;
public class NotificationActivity extends Activity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
Log.e("lele","onCreate: 进入NotificationActivity"); } }
|
MainActivity package a.b.c.mytoolbar;
import android.os.Bundle; import android.util.Log; import android.view.View;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.tb);
toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d("lele", "onClick: toolbar被点击了" ); } });
Toolbar toolbar2 = findViewById(R.id.tb2);
toolbar2.setNavigationIcon(R.drawable.baseline_arrow_back_24); toolbar2.setTitle("标题"); toolbar2.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.e("lele", "onClick: toolbar2被点击了" ); } });
} }
|
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical">
<androidx.appcompat.widget.Toolbar android:id="@+id/tb" android:layout_width="410dp" android:layout_height="match_parent" android:background="#ffff00" app:logo="@drawable/ic_launcher_foreground" app:navigationIcon="@drawable/baseline_arrow_back_24" app:subtitle="子标题" app:subtitleTextColor="#00ffff" app:title="标题" app:titleMarginStart="90dp" app:titleTextColor="#ff0000" />
<androidx.appcompat.widget.Toolbar android:id="@+id/tb2" android:layout_width="410dp" android:layout_height="match_parent"
android:background="#ffff00" />
<androidx.appcompat.widget.Toolbar android:layout_width="410dp" android:layout_height="match_parent" android:background="#ffff00" app:navigationIcon="@drawable/baseline_arrow_back_24"
> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" android:layout_gravity="center"
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
|
AlertDialog
MainActivity package a.b.c.myalertdialog;
import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.View;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "lele"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
}
public void click(View view) {
View dialogView = getLayoutInflater().inflate(R.layout.dialog_view,null); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher_foreground) .setTitle("我是对话框") .setMessage("你好啊") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Log.e(TAG, "onClick: 点击了确定" ); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Log.e(TAG, "onClick: 点击了取消" ); } }) .setNeutralButton("中间", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Log.e(TAG, "onClick: 点击了中间" ); } }) .setView(dialogView) .create() .show();
} }
|
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="显示对话框"/> </LinearLayout>
dialog_view.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher_foreground"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="今天天气怎么样"/> </LinearLayout>
|
MainActivity package a.b.c.mypopupwindow;
import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.PopupWindow;
import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void clidk(View view){ View popupView = getLayoutInflater().inflaer(R.layout.popup_view,null); Button btn1 = popupView.findViewById(R.id.btn1); Button btn2 = poputView.findViewById(R.id.btn2); PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CPNTENT,true); popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.tx)); popupWindow.showAsDropDown(view); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.e("lele", "onClick: 你是住在上海吗" ); popupWindow.dismiss(); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.e("lele", "onClick: 你是住在北京吗" ); popupWindow.dismiss(); } }); } }
|
activity_main <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="弹出PopupWindow"/>
</LinearLayout>
popup_view <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:background="@drawable/ic_launcher_background" >
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn1" android:padding="5dp" android:text="上海" android:textSize="18sp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn2" android:padding="5dp" android:text="北京" android:textSize="18sp"/>
</LinearLayout>
|