<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:id="@+id/rt1" 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.beijing.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <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="选择变色" android:onClick="click" /> </RelativeLayout>
package com.example.aaaaa; 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.TextView; import android.widget.Toast; public class MainActivity extends ActionBaractivity { public int tv = 0, r = 0, g = 0, b = 0; public int[] arr = { Color.rgb(0, 0, 255), Color.rgb(255, 0, 0),Color.rgb(255, 255, 255), Color.rgb(0, 0, 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 String[] { "蓝色", "红色", "白色", "黑色", "绿色" }, 0,new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // 点单选按钮时发生的事件,这里which表示你点的单选按钮是第几个 tv = which; } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点确定按钮时发生的事件 RelativeLayout rl = (RelativeLayout) findViewById(R.id.rt1); rl.setBackgroundColor(arr[tv]); Toast.makeText(MainActivity.this, "修改成功!!", 0).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog = builder.create(); dialog.show(); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。