1. TextView 和 EditText 控件經常使用屬性 java
android:layout_width="match_parent" 寬度與父控件同樣寬
android:layout_height="wrap_content" 包裹內容
android:layout_marginStart="8dp" 左外邊距8dp
android:layout_marginEnd="8dp" 右外邊距8dp
android:background="#FFFFFF" 背景色
android:ems="10"
android:gravity="top" 文字對齊
android:hint="說點什麼吧..."
android:inputType="textMultiLine" 多行輸入
android:lines="6" 顯示行數
android:padding="5dp" 內邊距
android:drawableStart="@drawable/addpicture" 在編輯框放一個圖片
2.Button 的 onClick 事件android
另外一個辦法,在代碼中建立按鈕的事件監聽器 數組
Button button = findViewById(R.id.button);
//給按鈕建立[事件監聽器]
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"單擊了按鈕",Toast.LENGTH_SHORT).show();
}
});
3.按鈕上設置 xml 背景app
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#1FBAF3"></solid> <corners android:radius="5dp"></corners> <padding android:bottom="10dp" android:left="15dp" android:right="15dp" android:top="10dp" ></padding> </shape>
按鈕屬性加上: android:background="@layout/shape"ide
4.圖片按鈕動畫
android:background="#0000" 背景透明ui
5.Activity 的切換this
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
finish(); //關閉Activity,返回到打開它的窗口, 若是是啓動窗口調用finish(),則直接退出程序
onCreate(null); //刷新窗口
直接修改啓動窗口,在清單文件中修改,將 Main2Activity 修改爲 其餘Activity,就會從其餘窗口先啓動,入口 Activityspa
<activity android:name=".Main2Activity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
6.Activity 之間數據傳遞 (Bundle Intent)code
窗口1代碼:
Button button = findViewById(R.id.buttonBunldIntent);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = ((EditText)(findViewById(R.id.editTextUserName))).getText().toString().trim();
String pwd = ((EditText)findViewById(R.id.editTextPasword)).getText().toString().trim();
String mobile = ((EditText)findViewById(R.id.editTextMobile)).getText().toString().trim();
if(name=="" || pwd=="" || mobile =="" ){
Toast.makeText(BundleIntentActivity.this,"用戶名、密碼、手機號 三者輸入不完整!",Toast.LENGTH_SHORT);
return;
}
Intent intent = new Intent(BundleIntentActivity.this,BundleIntentActivity2.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("name",name);
bundle.putString("pwd",pwd);
bundle.putString("mobile",mobile);
intent.putExtras(bundle);
startActivity(intent);
}
});
窗口2代碼:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
String mobile = bundle.getString("mobile");
String pwd = bundle.getString("pwd");
TextView tv = findViewById(R.id.textViewInfo);
tv.setText("name=" + name + "\n" + "pwd=" + pwd + "\nmobile=" + mobile);
6.Activity 返回數據
界面2,經過gridView 顯示頭像
第1個界面,經過 startActivityForResult(intent) 跳到第2個界面,經過事件onActivityResult接收返回的數據
第2個界面,setResult(0x11,intent) 返回數據
界面1代碼:
protected void onCreate(Bundle savedInstanceState) { //選擇頭像 findViewById(R.id.buttonSelHead).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,SelHeadActivity.class); startActivityForResult(intent,0x11); } }); } //返回數據顯示到圖片控件上 @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 0x11 && resultCode == 0x11){ Bundle bundle = data.getExtras(); int imageId = bundle.getInt("imageId"); ImageView imageView = findViewById(R.id.imageView); imageView.setImageResource(imageId); } }
界面2代碼:
package com.example; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; public class SelHeadActivity extends AppCompatActivity { //定義頭像數組 public int[] imageId = new int[]{R.drawable.tx1,R.drawable.tx2,R.drawable.tx3,R.drawable.tx4,R.drawable.tx5,R.drawable.tx6,R.drawable.tx7,R.drawable.tx8}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sel_head); GridView gridView = findViewById(R.id.gridViewHead); BaseAdapter adapter = new BaseAdapter() { @Override public int getCount() { return imageId.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; //獲取屏幕寬度 //獲取窗口管理器 WindowManager wm = getWindowManager(); int width = wm.getDefaultDisplay().getWidth(); int ImageWidth = width/4-10; if(convertView == null){ imageView = new ImageView(SelHeadActivity.this); imageView.setAdjustViewBounds(true); imageView.setMaxHeight(ImageWidth); imageView.setMaxWidth(ImageWidth); imageView.setPadding(5,5,5,5); } else{ imageView = (ImageView) convertView; } imageView.setImageResource(imageId[position]); return imageView; } }; gridView.setAdapter(adapter); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = getIntent(); Bundle bundle = new Bundle(); bundle.putInt("imageId",imageId[position]); //實例化要傳遞的數據包 intent.putExtras(bundle); //將數據包保存到 intent 中 setResult(0x11,intent); //設置返回的結果碼 finish(); //關閉當前Activity } }); } } 圖片放在gridView 控件中
7. Dialog 提示框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("回答錯誤,下面請看解析:當張山");
builder.setPositiveButton("肯定",null ).show();
8. android:layout_margin 與 padding
android:layout_margin 外邊距
padding 內邊距
9.設置成橫屏顯示
10.ImageView
adjustViewBounds 保持寬高比
hint 圖片着色
scaleType 縮放類型
11. ImageSwitcher 圖片切換器
package com.example;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import androidx.appcompat.app.AppCompatActivity;
public class ImageSwitchTest2 extends AppCompatActivity {
private int[] arrayPictures={R.drawable.tx1,R.drawable.tx2,R.drawable.tx3,R.drawable.tx4,R.drawable.tx5,R.drawable.tx6,R.drawable.tx7,R.drawable.tx8};
private ImageSwitcher imageSwitcher;
private int index;
private float touchDownX;
private float touchUpX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_switch_test2);
//設置全屏
Window window = getWindow();
WindowManager.LayoutParams winParams = window.getAttributes();
winParams.flags=WindowManager.LayoutParams.FLAG_FULLSCREEN;
window.setFlags(winParams.flags,winParams.flags);
imageSwitcher = findViewById(R.id.imageSwitcher1);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(ImageSwitchTest2.this);
imageView.setImageResource(arrayPictures[index]);
imageView.setMinimumWidth(200);
imageView.setMinimumHeight(200);
return imageView;
}
});
imageSwitcher.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
touchDownX = event.getX();
return true;
}
else if(event.getAction() == MotionEvent.ACTION_UP){
touchUpX = event.getX();
//從左到右
if(touchUpX - touchDownX>100){
index = (index == 0 ? arrayPictures.length-1:index-1);
//設置動畫資源
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitchTest2.this,R.anim.slide_in_right));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitchTest2.this,R.anim.slide_out_right));
imageSwitcher.setMinimumWidth(200);
imageSwitcher.setMinimumHeight(200);
imageSwitcher.setImageResource(arrayPictures[index]);
}
//從右到左
else if(touchDownX - touchUpX > 100){
index = (index == arrayPictures.length ? arrayPictures.length-1:index+1);
//設置動畫資源
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitchTest2.this,R.anim.slide_in_left));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitchTest2.this,R.anim.slide_out_left));
imageSwitcher.setMinimumWidth(200);
imageSwitcher.setMinimumHeight(200);
imageSwitcher.setImageResource(arrayPictures[index]);
}
}
return true;
}
});
}
}