Android小白,初次学习,笔记自用,敬请指正~😛 有帮助记得一键三连呀(点赞收藏关注)😁
1,activity_main.xml
<Button android:id="@+id/btn_btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button"/>2,MainActivity.java
private Button mBtnButton; mBtnButton = findViewById(R.id.btn_btn1); mBtnButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //跳转到Button演示界面 Intent intent= new Intent(MainActivity.this,ButtonActivity.class); startActivity(intent); } });3,activity_button.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="15dp"> <Button android:id="@+id/btn_1" android:layout_width="match_parent" android:layout_height="40dp" android:text="按钮1" android:textSize="20sp" android:textColor="#FFFFFF" android:background="#FF0000"/> <Button android:id="@+id/btn_2" android:layout_below="@id/btn_1" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="40dp" android:text="按钮2" android:textSize="20sp" android:textColor="#FF0000" android:background= "@drawable/bg_btn2"/> <Button android:id="@+id/btn_3" android:layout_below="@id/btn_2" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="40dp" android:text="按钮3" android:textSize="20sp" android:textColor="#FF9900" android:background="@drawable/bg_btn3"/> <Button android:id="@+id/btn_4" android:layout_below="@id/btn_3" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="40dp" android:text="按钮4" android:textSize="20sp" android:textColor="#FFFFFF" android:background="@drawable/bg_btn4" android:onClick="showToast"/> <TextView android:id="@+id/tv_1" android:layout_width="match_parent" android:layout_height="50dp" android:textColor="#000000" android:textSize="20sp" android:text="文字1" android:gravity="center" android:layout_below="@id/btn_4" android:layout_marginTop="40dp" android:background="#FFAD33"/> </RelativeLayout>4,ButtonActivity.java
package com.example.learning; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class ButtonActivity extends AppCompatActivity { private Button mBtn3;//1,声明控件 private TextView mTv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button); //点击事件btn3,方法二:(常用) mBtn3=findViewById(R.id.btn_3);//2,找到控件 mBtn3.setOnClickListener(new View.OnClickListener() {//3,点击事件 @Override public void onClick(View v) { Toast.makeText(ButtonActivity.this,"btn_3被点击了",Toast.LENGTH_SHORT).show(); } }); mTv1=findViewById(R.id.tv_1); mTv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(ButtonActivity.this,"tv_1被点击了",Toast.LENGTH_SHORT).show(); } }); } //点击事件btn4,方法一:(不常用) public void showToast(View view){ Toast.makeText(this,"btn_4被点击了",Toast.LENGTH_LONG).show(); } }5,bg_btn2.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FF9900"/> <corners android:radius="5dp"/> </shape>6,bg_btn3.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="1dp" android:color="#FF9900"/> <corners android:radius="5dp"/> </shape>7,bg_btn4.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <solid android:color="#AA6600"/> <corners android:radius="5dp"/> </shape> </item> <item android:state_pressed="false"> <shape> <solid android:color="#FF9900"/> <corners android:radius="5dp"/> </shape> </item> </selector>