• 05


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/ly1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.ground.MainActivity" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="设置背景颜色" />
    
    </RelativeLayout>
    package com.example.ground;
    
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.RelativeLayout;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.graphics.Color;
    import android.os.Bundle;
    
    
    public class MainActivity extends ActionBarActivity implements OnClickListener{
        private String[] colorArr={"#000000","#ffffff","#800080","#ffa500","#ffd700"}; 
        int textcolor=1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn=(Button)findViewById(R.id.button1);
            btn.setOnClickListener(this);
        }
      
        
        @Override
        public void onClick(View v) {
    
                AlertDialog dialog;
                AlertDialog.Builder builder = new AlertDialog.Builder(this)
                        .setTitle("设置背景颜色")           //设置标题
                        .setIcon(R.drawable.ic_launcher)
                        .setSingleChoiceItems(new String[]{"黑色", "默认", "紫色", "橙色",//黑色#000000 白色#ffffff紫色 #800080橙色#ffa500金色#ffd700                            
                                "金色"}, textcolor,new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                               // 点单选按钮时发生的事件,这里which表示你点的单选按钮是第几个
                            textcolor=which;
                            
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //点确定按钮时发生的事件                            
                                ((RelativeLayout)findViewById(R.id.ly1)).setBackgroundColor(Color.parseColor(colorArr[textcolor]));
                                dialog.dismiss();
                            }
                        })//添加“确定”按钮
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                               // 点取消按钮发生的事件
                                dialog.dismiss();
                            }
                        });
                dialog = builder.create();
                dialog.show();
            
        }
    
    
      
    }

  • 相关阅读:
    WINRAR发现溢出漏洞 3.6以下的版本全遭殃!
    Linux桌面即将奢华:KDE 4.1 Beta 1颁发
    AMD Catalyst 8.5 For Linux
    解说MySQL数据库的数据典范和建库战略
    FVWMCrystal:美观且易用的桌面环境
    Linux下建立ISO映像,运用ISO映像,卸载ISO映像
    KDiff3:文件及目次比拟/兼并东西
    Ubuntu 8.04装置nVidia新版显现驱动
    在linux浏览CHM文件
    SCN不差别将会招致ORA00600 2662错误
  • 原文地址:https://www.cnblogs.com/1014040868lyn/p/11574971.html
Copyright © 2020-2023  润新知