android 里面 对象,Android中Activity对象获取以及属性设定

这篇博客介绍了如何在Android应用中使用XML进行UI布局设计,并在Activity中实现按钮点击事件,包括改变按钮的文字颜色、背景色以及设置文字内容。示例展示了对两个按钮btn_test_1和btn_test_2的点击监听和样式修改。
摘要由CSDN通过智能技术生成

Android中UI界面在XML中约束,对象逻辑在Activity中赋值

public class MainActivity extends AppCompatActivity {

Button btn_test_2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

final Button btn_test_1 = findViewById(R.id.btn_1);

btn_test_1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

btn_test_1.setTextColor(Color.BLUE);

btn_test_1.setText("ddddddd");

btn_test_1.setBackgroundColor(Color.YELLOW);

}

});

btn_test_2 = findViewById(R.id.btn_2);

btn_test_2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

btn_test_2.setText("yyyyyy");

btn_test_2.setTextColor(getResources().getColor(R.color.colorAccent));

}

});

}

}

UI在activity_main.xml中编写

android:id="@+id/btn_1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_marginTop="100dp"

android:layout_marginLeft="50dp"

android:layout_marginRight="50dp"

android:layout_marginBottom="100dp"

android:background="@color/colorAccent"

/>

android:id="@+id/btn_2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="100dp"

android:layout_marginLeft="100dp"

android:height="100dp"

android:width="300dp"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintTop_toTopOf="parent"

android:background="@color/colorPrimary"

android:layout_marginStart="100dp"

/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值