Demo03:基本控件

来源:互联网 发布:网络教育需要到学校吗 编辑:程序博客网 时间:2024/06/04 17:49
package com.wuqiong.demo03;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.text.method.HideReturnsTransformationMethod;import android.text.method.PasswordTransformationMethod;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {private TextView mytext;private Button button1;private Button button2;private Button button3;private EditText edit;private CheckBox check;private static int num = 0;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mytext = (TextView) findViewById(R.id.mytext);        button1 = (Button) findViewById(R.id.button1);        button2 = (Button) findViewById(R.id.button2);        button3 = (Button) findViewById(R.id.button3);        edit = (EditText) findViewById(R.id.edit);        check = (CheckBox) findViewById(R.id.check);                button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mytext.setTextSize(24);}});                button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mytext.setTextSize(16);}});                button3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {num++;switch(num%4){case 1:mytext.setBackgroundColor(Color.RED);break;case 2:mytext.setBackgroundColor(Color.YELLOW);break;case 3:mytext.setBackgroundColor(Color.BLUE);break;case 0:mytext.setBackgroundColor(Color.GREEN);break;}}});                check.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if(check.isChecked())edit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());elseedit.setTransformationMethod(PasswordTransformationMethod.getInstance());}});    }    @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);    }}
<pre name="code" class="html"><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.wuqiong.demo03.MainActivity" >    <TextView        android:id="@+id/mytext"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:textSize="20sp"        android:text="@string/mytext" />    <Button         android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/mytext"        android:layout_alignParentLeft="true"        android:text="@string/enlarge"/>    <Button         android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/mytext"        android:layout_toRightOf="@id/button1"        android:text="@string/shrink"/>    <Button         android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/mytext"        android:layout_toRightOf="@id/button2"        android:text="@string/color"/><EditText     android:id="@+id/edit"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_below="@id/button1"    android:inputType="numberPassword"    android:hint="@string/type"/><CheckBox     android:id="@+id/check"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_below="@id/edit"    android:text="@string/display"/></RelativeLayout>



0 0
原创粉丝点击