zoukankan      html  css  js  c++  java
  • 第五次作业

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:id="@+id/f">
    
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="颜色选择"
            android:background="#00BCD4"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:onClick="gh"/>
    </RelativeLayout>
    

      

    package com.example.back;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.RelativeLayout;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    String b="#000000";
        public void gh(View view) {
            AlertDialog dialog;
            AlertDialog.Builder builder = new AlertDialog.Builder(this)
                    .setTitle("设置布局背景")
                    .setIcon(R.drawable.ic_launcher_background)
                    .setSingleChoiceItems(new String[]{"白色", "黑色", "红色", },
                            0,new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // 点单选按钮时发生的事件,这里which表示你点的单选按钮是第几个
                        switch (which){
                            case 0:b="#FAF9F9" ;
                            break;
                            case 1:b="#000000" ;
                            break;
                            case 2: b="#F44336";
                            break;
    
                        }
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //点确定按钮时发生的事件
                            ((RelativeLayout)findViewById(R.id.f)).setBackgroundColor(Color.parseColor(b));
                        dialog.dismiss();
                        }
                    })//添加“确定”按钮
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // 点取消按钮发生的事件
                            dialog.dismiss();
                        }
                    });
            dialog = builder.create();
            dialog.show();
        }
    }
    

      

  • 相关阅读:
    兼容FF IE的回车事件
    WP7页面的数值传递和对象传递
    简单实用选项卡
    离情切
    放在最前面
    Linux MySQL主从复制(Replication)配置
    C语言h文件头中的变量初始化
    verilog HDL 重复运算的问题
    SOPC构架双视频TFT液晶IP核
    用FPGA实现视频中物体边缘二值化
  • 原文地址:https://www.cnblogs.com/hanbing123/p/11572681.html
Copyright © 2011-2022 走看看