The first five jobs

 

 

 

 

 

 

 

 

 

 

 

import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    public int tv = 0, r = 0, g = 0, b = 0;
    public int[] arr = { Color.rgb(128,0,128), Color.rgb(255,192,203),Color.rgb(255,215,0
), Color.rgb(255,165,0), Color.rgb(0, 255, 0) };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click(View v) {
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("设置背景颜色")
                // 设置标题
                .setIcon (R.drawable.ic_launcher) 
                .setSingleChoiceItems ( new new String [] { "purple", "pink", "gold", "orange", "green"}, 0, new new DialogInterface.OnClickListener () {
                             public  void the onClick (DialogInterface Dialog,
                                     int Which) {
                                 // event occurs when point radio buttons, radio buttons showing Which here you are the first point of several 
                                TV = Which; 
                            } 
                        }) 
                .setPositiveButton ( "OK",newvoid DialogInterface.OnClickListener () {
                    @Override 
                    public  Which) { 
                        dialog.dismiss (); 
                    }the onClick (DialogInterface Dialog, int Which) {
                         // point event when the button is determined 
                        the RelativeLayout rl is an = (the RelativeLayout) the findViewById (R.id.rtl); 
                        rl.setBackgroundColor (ARR [TV]); 
                      
                    } 
                }) 
                .setNegativeButton ( " cancel ", new new DialogInterface.OnClickListener () { 
                    @Override 
                    public  void the onClick (DialogInterface Dialog, int 
                }); 
        Dialog = Once builder.create (); 
        with dialog.show (); 
    } 
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rtl"
    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.h4.MainActivity" >

   <Button
        android:id="@+id/but1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="188dp"
        android:onClick="click"
        android:text="设置背景颜色" />

   <TextView
       android:id="@+id/tv"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_above="@+id/but1"
       android:layout_centerHorizontal="true"
       android:layout_marginBottom="52dp"
       android:gravity="center"
       android:text="更换背景色"
       android:textColor="#FFC0CB"
       android:textSize="30sp" />

</RelativeLayout>

 

Guess you like

Origin www.cnblogs.com/Suzy-an/p/11580658.html