基础控件(Button,Edittext,ImageView,ProgressBar,ToolBar,AlertDialog,PopupWindow)
Button
触发事件
<?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">
<Button
android:id="@+id/btn"
android:onClick="myClick"
android:background="@drawable/btn_selector"
android:backgroundTint="@color/btn_color_selector"
android:layout_width="200dp"
android:layout_height="100dp"/>
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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; // 改为true时,执行onLongClick后不会执行onClick
}
});
// 触摸事件
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.e(TAG, "onTouch:" + motionEvent.getAction());
return false; // 改为true时,执行onTouch后不会执行onLongClick和onClick
}
});
}
public void myClick(View view) {
Log.e(TAG, "onClick:");
}
}
EditText
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/et"
android:hint="输入账号"
android:inputType="number"
android:drawableLeft="@drawable/ic_baseline_person_24"
android:drawablePadding="20dp"
android:paddingLeft="20dp"
android:textColorHint="#95a1aa"
android:layout_width="200dp"
android:layout_height="100dp"/>
<EditText
android:hint="输入密码"
android:inputType="textPassword"
android:textColorHint="#95a1aa"
android:layout_width="200dp"
android:layout_height="100dp"/>
<Button
android:id="@+id/btn"
android:text="获取账号"
android:layout_width="200dp"
android:layout_height="100dp"/>
</LinearLayout>
package com.example.myedittext;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "wmj";
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
et = findViewById(R.id.et);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str = et.getText().toString();
Log.e(TAG, "输入的账号为:" + str);
}
});
}
}
ImageView
<ImageView
android:src="@drawable/pic1"
android:scaleType="centerInside"
android:layout_width="200dp"
android:layout_height="200dp"/>
<ImageView
android:src="@drawable/pic2"
android:maxHeight="300dp"
android:maxWidth="300dp"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
ProgressBar
<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:onClick="myClick"
android:text="显示隐藏进度条"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/pb2"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:max="100"
android:indeterminate="true"
android:layout_width="300dp"
android:layout_height="100dp"/>
<Button
android:onClick="load"
android:text="模拟下载"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
package com.example.myprogressbar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private ProgressBar progressBar2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.pb);
progressBar2 = findViewById(R.id.pb2);
}
public void myClick(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);
}
}
ToolBar
- 在themes.xml中关闭toolbar:parent="Theme.MaterialComponents.DayNight.NoActionBar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.appcompat.widget.Toolbar
android:id="@+id/tb"
android:background="#ffff00"
app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
app:title="主标题"
app:titleTextColor="#ff0000"
app:titleMarginStart="50dp"
app:subtitle="子标题"
app:subtitleTextColor="#00ff00"
app:logo="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/tb2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="10dp"
android:background="#ffff00"/>
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="10dp"
app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
android:background="#ffff00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:text="标题"/>
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
package com.example.mytoolbar;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 注意是import androidx.appcompat.widget.Toolbar;
Toolbar toolbar = findViewById(R.id.tb);
Toolbar toolbar2 = findViewById(R.id.tb2);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("wmj", "点击了导航栏返回按钮");
}
});
toolbar2.setNavigationIcon(R.drawable.ic_baseline_arrow_back_24);
toolbar2.setTitle("标题");
toolbar2.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("wmj", "点了");
}
});
}
}
AlertDialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#FFFF00"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:text="哈哈"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示对话框"
android:onClick="myClick"/>
</LinearLayout>
package com.example.myalterdialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void myClick(View view) {
View dialog_view = getLayoutInflater().inflate(R.layout.dialog_view, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("对话框")
.setMessage("对话框内容")
.setView(dialog_view)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e("wmj", "确定");
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e("wmj", "取消");
}
})
.setNeutralButton("中间", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e("wmj", "中间");
}
}) // 以上返回的都是Builder,可以变换顺序
.create() // 返回的是AlertDialog,不能放在前面
.show();
}
}
PopupWindow
popup_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@mipmap/ic_launcher"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
android:padding="5dp"
android:textSize="18sp"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
android:padding="5dp"
android:textSize="18sp"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClick"
android:text="弹出窗口"/>
</LinearLayout>
package com.example.mypopupwindow;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void myClick(View view) {
View popupview = getLayoutInflater().inflate(R.layout.popup_view, null);
Button btn1 = popupview.findViewById(R.id.btn1);
Button btn2 = popupview.findViewById(R.id.btn2);
// 最后一个参数加上后,点击空白处popupwindow会消失
PopupWindow popupWindow = new PopupWindow(popupview, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, true);
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_baseline_airline_seat_recline_extra_24));
// popupWindow.showAsDropDown(view);
popupWindow.showAsDropDown(view, view.getWidth(), -view.getHeight());
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("wmj", "按钮1");
popupWindow.dismiss();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("wmj", "按钮2");
popupWindow.dismiss();
}
});
}
}