版權聲明:未經博主允許不得轉載 在Android中我們常常用到很多UI控制項,如 ,`EditText ImageView Button ImageButton ToggleButton CheckBox RadioButton`等等這些可以自己多用就會了。 也會學到一些佈局如: ,`Relative ...
版權聲明:未經博主允許不得轉載
在Android中我們常常用到很多UI控制項,如TextView
,EditText
,ImageView
,Button
,ImageButton
,ToggleButton
,CheckBox
,RadioButton
等等這些可以自己多用就會了。
也會學到一些佈局如:LinearLayout 線性佈局
,RelativeLayout 相對佈局
,FrameLayout 幀佈局
,AbsoluteLayout絕對佈局
,TableLayout 表格佈局
,GridLayout 網格佈局
,ConstraintLayout 約束佈局
。
LinearLayout 線性佈局
:
android:orientation="vertical"
android:orientation="horizontal"
RelativeLayout 相對佈局
:
android:layout_alignParentTop
android:layout_alignParentBottom
android:layout_alignParentLeft
android:layout_alignParentRight
android:layout_marginTop
android:layout_marginBottom
android:layout_marginLeft
android:layout_marginRight
android:layout_margin
android:layout_centerVertical
android:layout_centerHorizontal
android:layout_centerInParent
android:layout_above
android:layout_below
android:layout_alignLeft
android:layout_alignRight
android:layout_toLeftOf
android:layout_toRightOf
android:layout_alignBottom
android:layout_alignTop
android:layout_alignBaseline
TableLayout 表格佈局
:
android:layout_span
android:layout_column
android:collapseColumns
android:shrinkColumns
android:stretchColumns
屬性
android:id
android:layout_width
android:match_parent
android:text
android:background
android:textSize
android:textColor
android:inputType
android:hint
結構:
AndroidManifest.xml為整個應用的主配置,清單文件,應用的包名、版本號、組件、許可權等信息。
Activity的活動狀態:
activity is running:為活動狀態
onResume()->running->onPause():為暫停狀態
onStart()->onResume()->running->onPause()->onStop():為停止狀態
onDestroy():為非活動狀態
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
onRestart()
跳轉:第二個界面:
private Button mButton;
private String text="dashu";
mButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent data = new Intent();
data.putExtra("text",text);
setResult(2,data);
finish();
}
}
跳轉:第一個界面:
@Override
protected void onActivityResult(int requestCode,int resuleCode,Intent data){
super.onActivityResult(requestCode,resultCode,data):
if(requestCode==1&&resultCode==2){
String text = data.getStringExtra("text");
textView.setTextView(text);
}
}
ListView與ArrayAdapter的搭配:
//準備數據源->適配器載入數據源->控制項載入適配器
public class MainActivity extends AppCompatActivity{
private ListView listView;
private ArrayAdapter<String> list_adpater;
@Override
protected void onCreate(Bundle savedInsstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView=findViewById(R.id.listView);
//1
String data[] = {"dashu1","dashu2","dashu3"};
//2
list_adpater = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,data);
//3
listView.setAdapter(list_adapter);
}
}
ListView與SimpleAdapter的搭配:
//列表項
//佈局
<ListView
android:id="@android:id/app_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
//單個列表項
<ImageView
android:id="@+id/icon_image_view"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/title_text_view"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="@string/app_name"
android:textSize="24sp"/>
private ListView listView;
private List<Map<String,Object>> dataList;
private SimpleAdapter simple_adapter;
//1.
dataList = new ArrayList<>();
dataList=getData();
private List<Map<String,Object>> getData(){
for(int i = 0;i<10;i++){
Map<String,Object> map = new HashMap<>();
map.put("image",R.drawable.images1);
map.put("text",i);
dataList.add(map);
}
return dataList;
}
//2.
simple_adapter = new SimpleAdapter(this,dataList,R.layout.item,new String[]{"image","text"},new int[]{"R.id.imageView","R.id.imageView"});
//3.
listView.setAdapter(simple_adapter);
To:
public class App{
private String appName;
private int appId;
App(String appName,int appId){
}
}
//
public class AppAdapter extentds ArrayAdapter<App>{
...
App app = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(...);
}
//
//獲取視圖
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//創建ViewHolder
ViewHolder viewHolder = null;
//如果為空
if (convertView == null){
//mInflater.inflate(渲染佈局,parent , false)
convertView = mInflater.inflate(R.layout.item_msg,parent,false);
viewHolder = new ViewHolder();//創建類對象
//視圖
viewHolder.mIvImg = convertView.findViewById(R.id.id_iv_img);
viewHolder.mTvTitle = convertView.findViewById(R.id.id_tv_title);
viewHolder.mTvContent = convertView.findViewById(R.id.id_tv_content);
convertView.setTag(viewHolder);// convertView.getTag();
}else {
viewHolder = (ViewHolder) convertView.getTag();// convertView.getTag();
}
//獲取數據位置
Message msg = mDatas.get(position);//數據集合 private List<Message> mDatas;
viewHolder.mIvImg.setImageResource(msg.getImgResId());//獲取圖片位置
viewHolder.mTvTitle.setText(msg.getTitle());//獲取標題
viewHolder.mTvContent.setText(msg.getContent());//獲取內容
return convertView;
}
public static class ViewHolder{
//靜態內部類,類調用
ImageView mIvImg;
TextView mTvTitle;
TextView mTvContent;
}
//or
App app = getItem(position);
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder=new ViewHolder();
viewHolder.imageView= view.findViewById(R.id.app_image);
viewHolder.textView= view.findViewById(R.id.appl_name);
view.setTag(viewHolder);
}else{
view=convertView;
viewHolder = (ViewHolder) view.getTag();
...
return view;
}
To:
private List<Map<String,Object>> dataList;
private SimpleAdapter simpleAdapter;
private void getData(){
for(int i=0;i<##;i++){
Map<String,Object> map = new HashMap<>();
map.put("",##);
dataList.add(##);
}
}
ProgressBar
:max,progress,secondaryProgress
style="?android:attr/progressBarStyleHorizontal"
style="?android:attr/progressBarStyleLarge"
style="?android:attr/progressBarStyle"
style="?android:attr/progressBarStyleSamll"
int first=progressBar.getProgress();
int second=progressBar.getSecondaryProgress();
int max = progress.getMax();
progressBar.incrementProgressBy(5);
progressBar.incrementSecondaryProgressBy(5);
progress.incrementProgressBy(-5);
progressBar.incrementSecondaryProgressBy(-5);
ProgressDialog progressDialog = new ProgressDialog(this);
//
progressDialog.setProgresssStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("");
progressDialog.setIcon("");
//
progressDialog.setMax(100);
progressDialog.incrementProgressBy(20);
//
progessDialog.setButton(DialogInterface.BUTTON_POSITIVE,"確定",new DialogInterface.OnClickListener(){
...
});
progressDialog.setCancelable(true);
//
progressDialog.show();
SeekBar
:OnSeekBarChangeListener
<SeekBar>
...
android:max="100"
android:progress="40"
</SeekBar>
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
});
日曆選擇器:OnDateChangedListener
//DataPicker日曆選擇器
private DataPicker dataPicker;
private Calendar calendar;
private int year,day,hour,minute;
//
calendar = Calendar.getInstance();
//
year = calendar.get(Calendar.YEAR);
...
minute = calendar.get(Calendar.MINUTE);
//DataPickerDialog-OnDateSetListener()
new DatePickerDialog(this, newDatePickerDialog.OnDateSetListener(){
...
},year,calendar.get(Calendar.MONTH),day).show();
//TimePicker-setOnTimeChangedListener()
//TimePickerDialog-OnTimeSetListener
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener(){
....
},hour,minute,true).show();
Meau
:
//OptionsMenu,SubMenu,ContextMenu
//
<menu>
<item android:id="@+id/item1"
android:title="1"
</item>
<item android:id="@+id/item12"
android:title="12"
</item>
//
<item android:id="@+id/play" android:title="玩">
<menu>
<item android:id="" android:title="下棋"/>
<item .../>
</menu>
</item>
//
雷同SubMenu
//
getMenuInflater().inflate();
onCreateOptionsMenu();
onCreateContextMenu();
//動態
onCreateOptionsMenu(Menu menu){
menu.add(1,1,1,"1");
menu.add(1,2,1,"2");
return super.onCreateOptionsMenu(menu);
}
onCreateContextMenu(ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo){
//menu.setHeaderTitle();
//menu.setHeaderIcon();
menu.add(1,1,1,"1");
menu.add(1,2,1,"2");
...
super.onCreateContextMenu(menu,v,menuInfo);
}
//
onCreateOptionsMenu(Menu menu){
//SubMenu play=menu.addSubMenu();
play.setHeaderTitle();
play.setHeaderIcon();
play.add();
play.add();
SubMenu play2=menu.addSubMenu();
paly2.add(2,1,1,"");
play2.add(2,2,1,"");
return super.onCreateOptionsMenu(menu);
}
//
ContextMenu
ListView = findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
listView.setAdapter(adapter);
registerForContextMenu(listView);
//OnOptionsItemSelected
OnOptionsItemSelected(MenuItem item){
if(item.getGroupId() == 1){
switch(item.getItemId()){
case 1:
break;
case 2:
break;
}
}
...
}
Notification 通知
:
圖標(SmallIcon)
標題(ContextTitle)
內容(ContextText)
時間(When)
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//
builder.setSmallIcon();
...
builder.setDefaults(Notification.DEFAULT_ALL);
//
PendingIntent類-setContentIntent()
NotificationManager去完成發送和取消通知的事情
Dialog 對話框
:
//setPositiveButton()和setNegativeButton():添加確定和取消按鈕
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle();
...
builder.setPositiveButton("確認", new DialogInterface.OnClickListener(){
...
}
builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
...
}
Dialog dialog = builder.create();
dialog.show();
//setSingleChoiceItems()
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle();
builder.setIcon();
builder.setSingleChoiceItems(data,0,new DialogInterface.OnClickListener(){
...
});
Dialog dialog = builder.create();
dialog.show();
WebView
:
//uri , setAction , setData
Uri uri = Uri.parse(url);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
<WebView>
</WebView>
//
webView = findViewById(R.id.webView);
webView.loadUrl(url);
//
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
//
webView.setWebViewClient(new WebViewClient(){
...
});
//
webView.setWebChromeClient(new WebChromeClient(){
...
//
if(dialog != null && dialog.isShowing()){
dialog.dismiss():
dialog = null;
}
...
//
if(dialog == null){
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle();
...
dialog.show();
}else{
dialog.setProgress(newProgress);
}
}
Fragment
:為瞭解決Android碎片化
Fragment is added
onAttach()
onCreate()
onCreateView()
onActivityCreated()
onStart()
onResume()
Fragement is activite
onPause()
onStop()
onDestroyView();
onDestroy()
onDetach()
Fragment is destroyed
onCreate()
onCreateView()
onActivityCreated()
onStart()
onResume()
onPause()
onStop()
onStart()
onResume()
onPause()
onStop()
onDestroyView()
onDestroy()
onDetach()
//
MyFragment fragment = new MyFragment();
FragementManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.add(R.id.layout,fragment);
beginTransaction.commit();
//
getFragmentManager().beginTransaction().add(R.id.layout,fragment).commit();
//myFragment = (MyFragment)getFragmentManager(). findFragmentById(R.id.fragment);
//Fragment.java
Toast.makeText(getActivity(),"dashu",Toast.LENGTH_SHORT).show();
File,SharedPreferences,SQLite
private SQLiteDatabase db;
public class MyHelper extends SQLiteOpenHelper{
public static String CREATE_TABLE="create table student("
+ " _id integer primary key autoincrement,"
+ "name varchar(20),"
+ "age integer,"
+ "sex varchar(2)";
public Context mContext;
public MyHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version){
super(context,name,factory,version);
mContext=context;
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL();
}
//
Cursor cursor=db.query("student",null,null,null,null,null,null);
if(cursor!=null){
while(cursor.moveToNext()){
}
}
ContentProvider
Android四大組件之一,為存儲和獲取數據提供統一的介面。
自定義類繼承ContentProvider,通過android:authorities屬性授權。ContentResolver內容處理者,提供ContentProvider中同名同參的增刪改查方法。
boolean onCreate()
String getType(Uri uri)
Cursor query()
Uri insert()
int update()
int delete()
//URI : 完整的uri路徑
//
ContentProvider內容提供者
ContentResolver內容解析者
Spinner下拉框
<RelativeLayout>
<Spinner
android:id="@+id/spinner" ...
</RelativeLayout>
//android:entries="@array/list"
<resources>
<array name="list">
<item>1</item>
<item>2</item>
</array>
</resources>
public class MainActivity extends AppCompatActivity{
private Spinner mSpinner;
...
//
final String[] arrays = getResources().getStringArray(R.array.list);
//
ArrayAdapter<String> mSpinnerAdaper=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,arrays);
mSpinner.setAdapter(mSpinnerAdaper);
//setOnItemSelectedListener()
...
}
如果覺得不錯,那就點個贊吧!❤️
編輯 :達叔
定位:分享 Android&Java 知識點