作业5

<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.lh.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="164dp"
        android:text="选择背景色" 
              android:onClick="CCC"
              />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="" />
  
</RelativeLayout>


package com.example.lh;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;



public class MainActivity extends Activity {
private CharSequence[] items=new CharSequence[] {"红色","黄色","蓝色","绿色"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    
    
    
    
    
    
    
    int backcolor;
    String zh;
    public void CCC(View view) {
    
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("选择背景色")           //设置标题
                .setIcon(R.drawable.ic_launcher)
                .setSingleChoiceItems(new String[]{"红色", "黄色", "蓝色", "绿色"}, backcolor,new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                       // 点单选按钮时发生的事件,这里which表示你点的单选按钮是第几个
                        
                        if(which==0)
                            zh="#EA0000";
                        if(which==1)
                            zh="#FFFF37";
                        if(which==2)
                            zh="#0000E3";
                        if(which==3)
                            zh="#00BB00";
                        
                        
                    }
                })
                
                
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //点确定按钮时发生的事件                            
                         (findViewById(R.id.ly1)).setBackgroundColor(Color.parseColor(zh));
                        
                        
                    }
                })
                
                //添加“确定”按钮
                
                
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       // 点取消按钮发生的事件
                        
                        
                        
                    }
                });
        
        dialog = builder.create();
        dialog.show();
    
}

        
        
    }
    
    
    
    
    

 

posted @ 2019-09-23 13:42  郭少锋18840631471  阅读(116)  评论(0编辑  收藏  举报