美文网首页
Android中Activity对象获取以及属性设定

Android中Activity对象获取以及属性设定

作者: 汴城码农 | 来源:发表于2019-12-20 15:49 被阅读0次
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中编写
        <Button
            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"
            />
        <Button
            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" 
            />

相关文章

网友评论

      本文标题:Android中Activity对象获取以及属性设定

      本文链接:https://www.haomeiwen.com/subject/hyrinctx.html