一般新建组件有两种方式:XML中定义和Java代码实现,一般XML中定义较为常用。
按钮,在main.xml中定义如下:
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文本" android:id="@+id/button1" >
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(); //设置对话框属性并显示 } }和Button的区别为背景可以自定义图片,在main.xml中定义如下:
main.xml
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(); // 设置对话框属性并显示
}
});
}
}
main.xml
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();
}
}
}
}
main.xml
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();
}
}
}
}
main.xml
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();
}
}
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();
}
}
main.xml
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();
}
}
}
main.xml
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) {
}
});
}
}