<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: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.diwuci.MainActivity"
android:id="@+id/ly1">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="128dp"
android:text="Button" />
</RelativeLayout>
package com.example.diwuci;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements OnClickListener, android.view.View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
}
String s="#FFB6C1";
public void onClick(View v) {
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("设置背景颜色")
.setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(new String[]{"红色", "紫色", "蓝色", "黄色",
"金色"}, 2,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 点单选按钮时发生的事件,这里which表示你点的单选按钮是第几个
if(which==0)
((RelativeLayout)findViewById(R.id.ly1)).setBackgroundColor(Color.parseColor("#FFB6C1"));
if(which==1)
((RelativeLayout)findViewById(R.id.ly1)).setBackgroundColor(Color.parseColor("#FF00FF"));
if(which==2)
((RelativeLayout)findViewById(R.id.ly1)).setBackgroundColor(Color.parseColor("#0000FF"));
if(which==3)
((RelativeLayout)findViewById(R.id.ly1)).setBackgroundColor(Color.parseColor("#FFFF00"));
if(which==4)
((RelativeLayout)findViewById(R.id.ly1)).setBackgroundColor(Color.parseColor("FFD700"));
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//点确定按钮时发生的事件
}
})//添加“确定”按钮
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点取消按钮发生的事件
}
});
dialog = builder.create();
dialog.show();
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}