•參考資料
[1]:視頻資源
•效果展示圖
•前置知識
- TextView
- EditText
- Button 以及按壓效果,點擊事件
•出現的問題
輸入文本框 $Account$ 和 $Password$ 左側的圖標大小問題;
我是隨便在網上下載的兩張圖片,像素太大,以至於占用了太多的空間,如何修改 EditText 中圖標的大小呢?
參考資料:Android TextView設置圖標,調整圖標大小
首先,我在 $activity\_next.xml$ 中放置了兩個 $EditText$ 組件,設置的 $id$ 分別為 $et\_1$ , $et\_2$;
並把以下兩張圖片放在了 drawable 文件夾中,並分別取名為 $account.png$ , $password.png$;
![]()
$(account.png)$ $(password.png)$
其中,$account.png$ 放置在 $et\_1$ 組件中, $password.png$ 放置在 $et\_2$ 組件中;
在其對應的 $next\_activyti.java$ 文件中設置如下代碼即可改變其在 $EditText$ 中的大小;
1 EditText Et1 = findViewById(R.id.et_1); 2 Drawable icon = getResources().getDrawable(R.drawable.account);//找到account.png這張圖片 3 icon.setBounds(0,0,44,44); 4 Et1.setCompoundDrawables(icon, null, null, null); 5 6 EditText Et2 = findViewById(R.id.et_2); 7 Drawable password = getResources().getDrawable(R.drawable.password);//找到password.png這張圖片 8 password.setBounds(0,0,44,44); 9 Et2.setCompoundDrawables(password,null,null,null);$setBounds(left,top,right,bottom)$ 里的參數從左到右分別是:
- $drawable$ 的左邊到 $EditText$ 左邊緣 + $padding$ 的距離
- $drawable$ 的上邊離 $EditText$ 上邊緣 + $padding$ 的距離
- $drawable$ 的右邊離 $EditText$ 左邊緣 + $padding$ 的距離
- $drawable$ 的下邊離 $EditText$ 上邊緣 + $padding$ 的距離
相當於以 $EditText$ 邊框的左邊和上邊為參考,通過設置 $drawable$ 的位置來改變其顯示的大小;
•點擊事件
如上圖($gif$)所示,再點擊 $Sign in$ 按鈕的時候出現了點擊事件 $Login failed$;
只需在 $next\_activyti.java$ 添加如下代碼即可;
1 Btn1 = findViewById(R.id.btn_1);//btn_1 是 Sign in 按鈕的 id 2 Btn1.setOnClickListener(new View.OnClickListener(){//設置點擊事件 3 4 public void onClick(View view){ 5 Toast.makeText(nextActivity.this,"Login failed",Toast.LENGTH_SHORT).show(); 6 } 7 });
•完整代碼
activity_next.xml1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".nextActivity" 8 android:padding="10dp"> 9 10 <TextView 11 android:id="@+id/tv_1" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="Sign in to Hyacinth" 15 android:textSize="30sp" 16 android:textColor="#9E9191" 17 android:layout_centerHorizontal="true" 18 android:layout_marginTop="20dp"/> 19 20 <EditText 21 android:id="@+id/et_1" 22 android:layout_width="match_parent" 23 android:layout_height="50dp" 24 android:layout_below="@id/tv_1" 25 android:layout_marginTop="60dp" 26 android:textSize="20sp" 27 android:textColor="#FFAD33" 28 android:hint="Account" 29 android:background="@drawable/ic_btn3" 30 android:paddingLeft="15dp" 31 android:drawableLeft="@drawable/account" 32 android:drawablePadding="10dp" 33 /> 34 35 <EditText 36 android:id="@+id/et_2" 37 android:layout_width="match_parent" 38 android:layout_height="50dp" 39 android:layout_below="@id/et_1" 40 android:layout_marginTop="10dp" 41 android:textSize="20sp" 42 android:textColor="#FFAD33" 43 android:hint="Password" 44 android:inputType="textPassword" 45 android:background="@drawable/ic_btn3" 46 android:paddingLeft="15dp" 47 android:drawableLeft="@drawable/password" 48 android:drawablePadding="10dp" 49 /> 50 <Button 51 android:id="@+id/btn_1" 52 android:layout_width="match_parent" 53 android:layout_height="50dp" 54 android:layout_below="@id/et_2" 55 android:layout_marginTop="40dp" 56 android:background="@drawable/ic_btn_sel" 57 android:text="Sign in" 58 android:textAllCaps="false" 59 /> 60 61 </RelativeLayout>
nextActivity.java1 public class nextActivity extends AppCompatActivity { 2 3 private Button Btn1; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_next); 9 10 EditText Et1 = findViewById(R.id.et_1); 11 Drawable icon = getResources().getDrawable(R.drawable.account); 12 icon.setBounds(0,0,44,44); 13 Et1.setCompoundDrawables(icon, null, null, null); 14 15 EditText Et2 = findViewById(R.id.et_2); 16 Drawable password = getResources().getDrawable(R.drawable.password); 17 password.setBounds(0,0,44,44); 18 Et2.setCompoundDrawables(password,null,null,null); 19 20 Btn1 = findViewById(R.id.btn_1); 21 Btn1.setOnClickListener(new View.OnClickListener(){ 22 23 public void onClick(View view){ 24 Toast.makeText(nextActivity.this,"Login failed",Toast.LENGTH_SHORT).show(); 25 } 26 }); 27 } 28 }