作业5

社会主义新天地 提交于 2019-11-30 10:45:18

 

 

 

 

 

 

 

 

 

 

<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"
     android:id="@+id/beijing"
    tools:context="com.example.sxz1.MainActivity" >

  <Button
      android:id="@+id/bu"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="166dp"
      android:text="改变颜色" />

</RelativeLayout>
package com.example.sxz1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button wbu = (Button) findViewById(R.id.bu);
        wbu.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    String color;
    @Override
    public void onClick(View v) {
        
        // TODO Auto-generated method stub
            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表示你点的单选按钮是第几个
                switch (which) {
                case 0:
                    color="#ffffff";
                    break;
                case 1:
                    color="#000000";
                    break;
                case 2:
                    color="#e22e2b";
                    break;
                default:
                    color="#ff00ff";
                    break;
                }
                
            }
            })
            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            //点确定按钮时发生的事件
                (findViewById(R.id.beijing)).setBackgroundColor(Color.parseColor(color));
                
            }
            })//添加“确定”按钮
            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            }
            });
            dialog = builder.create();
            dialog.show();
        }
    }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!