一般新建组件有两种方式:XML中定义和Java代码实现,一般XML中定义较为常用。
按钮,在main.xml中定义如下:
<Button
android:layout_width="wrap_content" <!--按钮宽度匹配文本的大小 -->
android:layout_height="wrap_content" <!--按钮高度匹配文本大小 -->
android:text="文本" <!--按钮的文本 -->
android:id="@+id/button1" <!--按钮的id -->
></Button>
ButtonActivity.java
package org.xiazdong;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ButtonActivity extends Activity implements OnClickListener{ //实现点击监听器
private Button button;
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button1); //根据ID找组件
tv = (TextView)findViewById(R.id.tv);
button.setOnClickListener(this); //为button设置监听器
}
@Override
public void onClick(View view) {
String str = new Random().nextInt()+"";
tv.setText(str);
Toast.makeText(this, "点击了按钮!!", Toast.LENGTH_SHORT).show(); //设置提示信息
Builder builder = new AlertDialog.Builder(this); //创建对话框
builder.setTitle("提示信息").setMessage("点击了按钮,随机数为:"+str).show(); //设置对话框属性并显示
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/tv"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击生成随机数"
android:id="@+id/button1"
></Button>
</LinearLayout>
和Button的区别为背景可以自定义图片,在main.xml中定义如下:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ib1"
android:background="@drawable/ic_launcher"/> <!--设置按钮的背景为drawable文件夹下的ic_launcher图片 -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ib1"
android:background="@drawable/ic_launcher"/>
</LinearLayout>
package org.xiazdong;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
public class ImageButtonActivity extends Activity {
private ImageButton ib1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ib1 = (ImageButton) findViewById(R.id.ib1);
ib1.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){ //按下按钮时
ib1.setBackgroundResource(R.drawable.logo);
}
else if(event.getAction()==MotionEvent.ACTION_UP){ //抬起按钮时
ib1.setBackgroundResource(R.drawable.ic_launcher);
}
return false;
}
});
}
}
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="输入用户名..."
android:inputType=""
/>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" /> <EditText android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="输入用户名..." android:inputType="" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" /> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="输入密码..." android:password="true" /> </LinearLayout> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="提交" > </Button> </LinearLayout>
EditTextActivity.java
package org.xiazdong; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class EditTextActivity extends Activity { private EditText name; private EditText password; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); name = (EditText) findViewById(R.id.name); button = (Button) findViewById(R.id.button); password = (EditText) findViewById(R.id.password); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String n = name.getText().toString(); String p = password.getText().toString(); Builder builder = new AlertDialog.Builder(EditTextActivity.this); // 创建对话框 builder.setTitle("提示信息").setMessage("用户名:" + n + "\n密码:" + p) .setPositiveButton("知道了", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { password.setText(""); //清空密码 } }).show(); // 设置对话框属性并显示 } }); } }
<CheckBox
android:id="@+id/shanghai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="城市:" /> <CheckBox android:id="@+id/shanghai" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上海" /> <CheckBox android:id="@+id/beijing" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="北京" /> <CheckBox android:id="@+id/tianjing" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="天津" /> </LinearLayout>
CheckBoxActivity.java
package org.xiazdong; import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Toast; public class CheckBoxActivity extends Activity implements OnCheckedChangeListener { private CheckBox cb1, cb2, cb3; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); cb1 = (CheckBox) findViewById(R.id.shanghai); cb2 = (CheckBox) findViewById(R.id.beijing); cb3 = (CheckBox) findViewById(R.id.tianjing); cb1.setOnCheckedChangeListener(this); cb2.setOnCheckedChangeListener(this); cb3.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, //buttonView表示改变的框,isChecked表示是选中还是取消选中 boolean isChecked) { if(buttonView==cb1||buttonView==cb2||buttonView==cb3){ if(isChecked){ Toast.makeText(this, buttonView.getText()+"被选中",Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(this, buttonView.getText()+"取消选中",Toast.LENGTH_SHORT).show(); } } } }
<RadioGroup>
<RadioButton
android:id="@+id/rb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton1" >
</RadioButton>
<RadioButton>
</RadioButton>
......
</RadioGroup>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RadioGroup android:id="@+id/rg1" android:layout_width="fill_parent" android:layout_height="wrap_content" > <RadioButton android:id="@+id/rb1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="男" > </RadioButton> <RadioButton android:id="@+id/rb2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="女" > </RadioButton> </RadioGroup> </LinearLayout>
RadioButtonActivity.java
package org.xiazdong; import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import android.widget.RadioGroup.OnCheckedChangeListener; public class RadioButtonActivity extends Activity implements OnCheckedChangeListener{ /** Called when the activity is first created. */ private RadioButton rb1,rb2; private RadioGroup rg; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rb1 = (RadioButton)findViewById(R.id.rb1); rb2 = (RadioButton)findViewById(R.id.rb2); rg = (RadioGroup)findViewById(R.id.rg1); rg.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if(group==rg){ if(rb1.getId()==checkedId){ Toast.makeText(this, rb1.getText(), Toast.LENGTH_SHORT).show(); } if(rb2.getId()==checkedId){ Toast.makeText(this, rb2.getText(), Toast.LENGTH_SHORT).show(); } } } }
<ProgressBar
android:id="@+id/pb1"
style="?android:attr/progressBarStyleXxx" <!--设置进度条的样式,有大、中、小、条状 -->
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/pb4" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:max="100" android:progress="0" android:secondaryProgress="0" /> </LinearLayout>
ProgressBarActivity.java
package org.xiazdong; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.ProgressBar; import android.widget.Toast; public class ProgressBarActivity extends Activity implements Runnable { private ProgressBar bar; private boolean isFinished; Thread t; Handler handler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bar = (ProgressBar) findViewById(R.id.pb4); t = new Thread(this); t.start(); } public void showToast() { handler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "安装完成!", //此处需要使用Handler,因为不能在子线程中使用Toast Toast.LENGTH_SHORT).show(); } }); } public void run() { int current = bar.getProgress(); int currentMax = bar.getMax(); int secCurrent = bar.getSecondaryProgress(); while (true) { bar.setProgress(current++); bar.setSecondaryProgress(secCurrent++); if (secCurrent >= currentMax) { break; } try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } isFinished = true; showToast(); } }
7.TextView
文本显示组件,在main.xml中定义如下:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" /> <!--文本文字 -->
Builder builder = new Builder(DialogActivity.this); //创建对话框 builder.setIcon(android.R.drawable.ic_dialog_info).setTitle("对话框标题"); //设置对话框图标和标题 builder.setMessage("对话框内容"); //设置对话框信息 builder.setPositiveButton("Yes", new OnClickListener(){ //设置正确按钮 @Override public void onClick(DialogInterface dialog, int arg1) { } }); builder.setNegativeButton("No", new OnClickListener(){ //设置否定按钮 @Override public void onClick(DialogInterface dialog, int arg1) { } }); builder.show(); //显示对话框
package org.xiazdong; import android.app.Activity; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; public class DialogActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Builder builder = new Builder(DialogActivity.this); builder.setMultiChoiceItems(new String[] { "上海", "北京", "天津" }, //每项内容 new boolean[] { true, false, true }, //每项是否没选中 new OnMultiChoiceClickListener() { //监听器 @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { } }).show(); } }(3)在dialog中添加列表builder.setItems(new String[]{"项1","项2"},new OnClickListener(){});(4)在dialog中添加视图(在main.xml中定义):setView函数实现;Builder builder = new Builder(DialogActivity.this); View layout = LayoutInflater.from(this).inflate(R.layout.main, null); builder.setIcon(android.R.drawable.ic_dialog_info).setTitle("对话框标题"); builder.setMessage("对话框内容"); builder.setPositiveButton("Yes", new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int arg1) { } }); builder.setNegativeButton("Yes", new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int arg1) { } }); builder.setView(layout); builder.show();
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/l1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第1页"></TextView> </LinearLayout> <LinearLayout android:id="@+id/l2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第2页"></TextView> </LinearLayout> <LinearLayout android:id="@+id/l3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第3页"></TextView> </LinearLayout> </LinearLayout>TabHostActivity.java
package org.xiazdong; import android.app.TabActivity; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; import android.widget.TabHost.TabSpec; import android.widget.Toast; public class TabHostActivity extends TabActivity implements OnTabChangeListener { //继承TabActivity而不是Activity TabHost host; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); host = this.getTabHost(); //新建TabHost LayoutInflater.from(this).inflate(R.layout.main, //将main布局文件映射成tabHost的view host.getTabContentView()); TabSpec t1 = host.newTabSpec("t1"); //新建一个页,id为t1 t1.setIndicator("标签1"); //设置显示页名 t1.setContent(R.id.l1); //设置页的内容为l1布局,此处可以是布局或组件 host.addTab(t1); //加入TabHost中 TabSpec t2 = host.newTabSpec("t2"); t2.setIndicator("标签2",getResources().getDrawable(R.drawable.ic_launcher)); t2.setContent(R.id.l2); host.addTab(t2); TabSpec t3 = host.newTabSpec("t3"); t3.setIndicator("标签3"); t3.setContent(R.id.l3); host.addTab(t3); host.setOnTabChangedListener(this); //设置监听器 } @Override public void onTabChanged(String tabId) { Log.v("a","aaaa"); if(tabId.equals("t1")){ Toast.makeText(this, "标签1ing", Toast.LENGTH_LONG).show(); } if(tabId.equals("t2")){ Toast.makeText(this, "标签2ing", Toast.LENGTH_LONG).show(); } if(tabId.equals("t3")){ Toast.makeText(this, "标签3ing", Toast.LENGTH_LONG).show(); } else{ Toast.makeText(this, tabId, Toast.LENGTH_LONG).show(); } } }
<SeekBar android:id="@+id/sb" android:layout_width="fill_parent" android:layout_height="wrap_content" />
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/tv" /> <SeekBar android:id="@+id/sb" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>SeekBarActivity.java
package org.xiazdong; import android.app.Activity; import android.os.Bundle; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; public class SeekBarActivity extends Activity { private TextView tv; private SeekBar sb; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.tv); sb = (SeekBar) findViewById(R.id.sb); sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { tv.setText(progress+""); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } }11.ListView
列表视图;(1)使用ArrayAdapter实现普通列表
ArrayAdapter是一个媒介,通过它可以把数组映射到ListView视图上。(1)new ArrayAapter<String>(this,android.R.layout.simple_list_item_1,list); 将list存放到ArrayAdapter中;(2)lv.setAdapter(adapter); 为listView设置Adapter;package org.xiazdong; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class ListViewActivity extends Activity implements OnItemClickListener{ ArrayList<String> list; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); list = new ArrayList<String>(); list.add("xiazdong-1"); list.add("xiazdong-2"); list.add("xiazdong-3"); ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list); ListView lv = new ListView(this); lv.setAdapter(adapter); lv.setOnItemClickListener(this); this.setContentView(lv); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(this,list.get(arg2), Toast.LENGTH_SHORT).show(); } }
(2)自定义适配器BaseAdapter
二、4种布局介绍
AbsoluteLayout因为已被废除,因此不做介绍;
只要存在界面,就会有布局的存在,就像Swing,虽然一个是桌面应用,一个是手机应用,但是他们都差不多。
此处因为布局非常简单,所以就不用代码来讲解了。
1.LinearLayout
默认布局。组件的排列按照预先定义方向很有序的排列,类似于Swing中的FlowLayout;
注意点:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第一个界面" /> <TextView android:id="@+id/tv1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> <EditText android:id="@+id/e1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="输入信息" /> <Button android:id="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送到第二个界面" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第二个界面" /> <TextView android:id="@+id/tv2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> <EditText android:id="@+id/e2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="输入信息" /> <Button android:id="@+id/b2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送到第一个界面" /> </LinearLayout>
package org.xiazdong; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MultiActivityActivity extends Activity implements OnClickListener{ private Button b1; private EditText e1; private TextView tv1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); b1 = (Button)findViewById(R.id.b1); e1 = (EditText)findViewById(R.id.e1); tv1 = (TextView)findViewById(R.id.tv1); Intent i = this.getIntent(); if(i.getStringExtra("2")!=null){ tv1.setText(i.getStringExtra("2")); } b1.setOnClickListener(this); } @Override public void onClick(View v) { Intent intent = new Intent(MultiActivityActivity.this,OtherActivity.class); intent.putExtra("1", e1.getText().toString()); this.startActivity(intent); } }
package org.xiazdong; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class OtherActivity extends Activity implements OnClickListener{ private TextView view ; private Button b2; private EditText e2; private TextView tv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); view = new TextView(this); setContentView(R.layout.mylayout); b2 = (Button)findViewById(R.id.b2); e2 = (EditText)findViewById(R.id.e2); tv2 = (TextView)findViewById(R.id.tv2); Intent i = this.getIntent(); if(i.getStringExtra("1")!=null){ tv2.setText(i.getStringExtra("1")); } b2.setOnClickListener(this); } @Override public void onClick(View v) { Intent intent = new Intent(OtherActivity.this,MultiActivityActivity.class); intent.putExtra("2", e2.getText().toString()); this.startActivity(intent); } }