首页 > 代码库 > Android(2)

Android(2)

1.Android资源文件

构建应用程序需要丰富多彩的色彩,在Android平台中可以将相关颜色信息配置在color.xml文件中。

例:

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

    <color name="green">#00EE00</color>

    <color name="black">#000000</color>

    <color name="red">#FF0000</color>

    <color name="blue">#0000FF</color>

    <color name="white">#FFFFFF</color>

</resources>

字符串常量文件

为了更好的国际化和本地化,Android平台建议将相关字符串常量放置在res/string.xml中。

例:

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

    <string name="app_name">多彩手电筒</string>

    <string name="action_settings">Settings</string>

    <string name="hello_world">Hello world!</string>

 

</resources>

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/relayout"

    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:background="@color/black"

    tools:context=".MainActivity" >

 

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginLeft="30dp"

        android:layout_marginTop="41dp"

        android:background="@color/white"

        android:text="红色" />

 

    <Button

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/button1"

        android:layout_below="@+id/button1"

        android:layout_marginTop="37dp"

        android:background="@color/white"

        android:text="绿色" />

 

    <Button

        android:id="@+id/button3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/button2"

        android:layout_below="@+id/button2"

        android:layout_marginTop="44dp"

        android:background="@color/white"

        android:text="蓝色" />

 

</RelativeLayout>

 

MainActivity.java

package com.example.bulb;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
private Button btn1, btn2, btn3;
private RelativeLayout relayout;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) this.findViewById(R.id.button1);
btn2 = (Button) this.findViewById(R.id.button2);
btn3 = (Button) this.findViewById(R.id.button3);
relayout = (RelativeLayout) this.findViewById(R.id.relayout);
 
btn1.setOnClickListener(new OnClickListener() {
 
@Override
public void onClick(View arg0) {
// Toast.makeText(MainActivity.this, "11111111111",
// 10000).show();
 
relayout.setBackgroundColor(getResources()
.getColor(R.color.red));
}
});
 
btn2.setOnClickListener(new OnClickListener() {
 
@Override
public void onClick(View arg0) {
relayout.setBackgroundColor(getResources().getColor(
R.color.green));
 
}
});
 
btn3.setOnClickListener(new OnClickListener() {
 
@Override
public void onClick(View arg0) {
relayout.setBackgroundColor(getResources().getColor(
R.color.blue));
}
});
 
}
 
@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;
}
 
}

运行结果为:



2.RadioButton单选按钮

<RadioGroup
      android:id="@+id/radioGroup1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@+id/sing"
      android:layout_below="@+id/dancer"
      android:layout_marginRight="30dp"
      android:layout_marginTop="19dp"
      android:orientation="horizontal" >
 
      <RadioButton
          android:id="@+id/radio0"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:checked="true"
          android:text="男" />

      <RadioButton
          android:id="@+id/radio1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="女" />
  </RadioGroup>


3.小案例:

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">测试程序</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="btnok">确定</string>
    <string name="btncancel">取消</string>
    <string name="poems"><i>菩提本无树,明镜亦非台。</i>\n<u>本来无一物,何处惹尘埃</u></string>
    

</resources>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <color name="green">#00EE00</color>
    <color name="red">#FF0000</color>
</resources>

array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="provices">
        <item >河北</item>
        <item>上海</item>
        <item>北京</item>
    </string-array>
</resources>

activity_main.xml

<RelativeLayout
    android:id="@+id/relayout"
     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:background="@color/red"
    tools:context=".MainActivity" >

      <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="148dp"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="26dp"
        android:layout_marginRight="40dp"
        android:text="@string/btnok"
        />

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView"
        android:layout_below="@+id/textView"
        android:layout_marginTop="64dp"
        android:numStars="6"
        android:rating="5"
        android:scaleX="0.5"
        android:stepSize="1" />

</RelativeLayout>

MainActivity.java

package com.csdn.he;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private Button btn;
	private TextView text;
	private RatingBar bar;
	private RelativeLayout  relayout; 
	private float count;
	private ImageView iv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn = (Button) this.findViewById(R.id.btn);
		text = (TextView) this.findViewById(R.id.textView);
		bar = (RatingBar) this.findViewById(R.id.ratingBar1);
		relayout=(RelativeLayout) this.findViewById(R.id.relayout);
		
		count = 4;
		text.setText("你好!");
		btn.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.activity_main, menu);
		return true;
	}

	@SuppressLint("ResourceAsColor") @Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		text.setText("点击按钮!");
		bar.setRating(count);
		count--;

		// 设置文本框内容为当期日期
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		text.setText(format.format(date).toString());
		//修改颜色
		relayout.setBackgroundColor(getResources().getColor(R.color.green));
		text.setText(R.string.poems);
		
		
		String[] privinces = getResources().getStringArray(R.array.provices);
		for(int i=0;i<privinces.length; i++)
		{
			Toast.makeText(MainActivity.this, privinces[i], 10000).show();
		}
	}
	
}