<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">安卓初学者。</span>
安卓项目文件夹目录:
src:存放所有Java源代码
gen:存放所有自动生成的文件,最重要的是R.java。R.java记录应用程序用到的资源,并为其制作索引列表。
asserts:存放不产生索引的外部资源
res:资源文件夹。drawable用来存放图片资源,layout用来存放界面设置的XML文件,values用来存放参数设置的XML文件。
TextView,文本控件。
TextView语法格式:
<TextView
android:id="@+id/myTextView"
android:text="这是TextView控件"
android:textColor="#00ffff"
android:textSize="20dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
EditText,编辑文本控件。
EditText语法格式:
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
EditText常用属性:
Lines,对应XML代码:android:lines,设置最多可以显示的行数
Editable,对应XML代码:android:editable,是否可输入文字
Enabled,对应XML代码:android:enabled,设置组件是否可用
Password,对应XML代码:android:password,文字输入后以密码显示输入文字
Phonenunber,对应XML代码:android:phoneNumber,只可以输入电话号码
Singleline,对应XML代码:android:singleline,false可输入多行,true只能输入一行
InputType,对应XML代码:android:inputType,用来限定输入数据的类型
Hint,对应XML代码:android:hint,输入字段为空时候显示的内容
多按钮共享事件:(举个例子)
布局XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数字:" />
<Button
android:id="@+id/button0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0"/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="1"/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="2"/>
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="3"/>
<Button
android:id="@+id/clear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="清除"/>
</LinearLayout>
Java文件:
package com.example.c2;
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.TextView;
public class MainActivity extends Activity {
Button btn0, btn1, btn2, btn3, clear;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn0 = (Button)findViewById(R.id.button0);
btn1 = (Button)findViewById(R.id.button1);
btn2 = (Button)findViewById(R.id.button2);
btn3 = (Button)findViewById(R.id.button3);
clear = (Button)findViewById(R.id.clear);
//五个按钮共享一个事件l
btn0.setOnClickListener(l);
btn1.setOnClickListener(l);
btn2.setOnClickListener(l);
btn3.setOnClickListener(l);
clear.setOnClickListener(l);
txt = (TextView)findViewById(R.id.txt);
}
OnClickListener l = new OnClickListener() //声明l是OClickListener类的一个对象
{
public void onClick(View v)
{
String s = txt.getText().toString();
switch(v.getId())
{
case R.id.button0:
{
txt.setText(s + "0");
break;
}
case R.id.button1:
{
txt.setText(s + "1");
break;
}
case R.id.button2:
{
txt.setText(s + "2");
break;
}
case R.id.button3:
{
txt.setText(s + "3");
break;
}
case R.id.clear:
{
txt.setText("数字:");
break;
}
}
}
};
@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;
}
}
效果图:
基本用法:
Toast 变量名称 = Toast.makeText(主程序类.this, Text, Time);
变量名称.show();
一般情况下,使用匿名对象较为简洁:
Toast.makeText(主程序类.this, Text, Time).show();
Toast.makeText(MainActivity.this, "你按了按钮一", Toast.LENGTH_LONG).show();
Alert.Dialog对话框是弹出消息框,提示用户是否进行操作
语法格式:
AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
b.setTitle("确认窗口")
.setMessage(s1 + " 按钮一 \n" + s2)
.setPositiveButton("确定", null)
.show();
也可以使用匿名对象:
new AlertDialog.Builder(MainActivity.this)
.setTitle("确认窗口")
.setMessage(s1 + " 按钮一 \n" + s2)
.setPositiveButton("确定", null)
.show();
效果: