一.EditText
1.EditText基础
<EditText
android:id="@+id/et"
android:hint="请输入用户名"//文本填充,点击消失
android:textColorHint="#95a1aa"
android:inputType="phone"//内容格式
android:drawableLeft="@drawable/ic_baseline_emoji_people_10"
android:drawablePadding="10dp"//人像图标与文本框间距
android:paddingLeft="10dp"//图标与手机左侧间隔
android:background="#00000000"
android:layout_width="200dp"
android:layout_height="100dp"
/>
显示结果:
?2.设置响应
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn"
android:text="获取用户名"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
private EditText et;
Button btn = findViewById(R.id.btn);
et = findViewById(R.id.et);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s=et.getText().toString();
Log.e("la" , "输入的内容: "+s );
}
});
显示结果:
?二.ImageView
?设置图片自适应
<ImageView
android:src="@drawable/test"
android:scaleType="fitXY"//缩放填充
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="200dp"
android:maxWidth="200dp"
android:adjustViewBounds="true"
/>
三.ProgressBar进度条
1.转圈进度条+进度条显示隐藏
<ProgressBar
android:id="@+id/pd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:onClick="leoClick"
android:text="显示隐藏进度条"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
private ProgressBar progressBar;
progressBar = findViewById(R.id.pd);
public void leoClick(View view) {
if(progressBar.getVisibility()==View.GONE){//隐藏
progressBar.setVisibility(View.VISIBLE);//显示
}
else
{
progressBar.setVisibility(View.GONE);
}
}
2.水平进度条+进度模拟
<ProgressBar
android:id="@+id/pd2"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:max="100"
android:layout_width="300dp"
android:layout_height="wrap_content"
/>
<Button
android:onClick="load"
android:text="增加进度条进度"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
private ProgressBar progressBar2;
progressBar2 = findViewById(R.id.pd2);
public void load(View view) {
int progress = progressBar2.getProgress();//获取进度
progress+=10;//进度加10
progressBar2.setProgress(progress);//设置进度
}
3.循环进度条
<ProgressBar
android:indeterminate="true"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:max="100"
android:layout_width="300dp"
android:layout_height="wrap_content"
/>
显示结果:
四.消息通知Notification
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发出通知"
android:onClick="sendNotification"
/>
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消通知"
android:onClick="cancelNotification"
/>
public class MainActivity extends AppCompatActivity {
private NotificationManager manager;
private Notification notification;//设置为全局
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//R文件自动生成,不可修改,创建的资源都会在R文件中生成一个索引
manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){//安卓8.0以上才需要设置通知渠道channel
NotificationChannel channel = new NotificationChannel("la","测试通知",NotificationManager.IMPORTANCE_HIGH);//id需要和通知中的channelId相同
manager.createNotificationChannel(channel);
}
Intent intent = new Intent(this,NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
notification = new NotificationCompat.Builder(this,"la")//设置通知
.setContentTitle("官方通告")
.setContentText("lalala")
.setSmallIcon(R.drawable.ic_baseline_emoji_people_10)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.test1))
.setColor(Color.parseColor("#ff000000"))
.setContentIntent(pendingIntent)//点击消息会跳转到页面
.setAutoCancel(true)//点击消息跳转后消息会消失
.build();
//channel通知渠道
}
public void sendNotification(View view) {
manager.notify(1,notification);
}
public void cancelNotification(View view) {
manager.cancel(1);
}
}
|