Android教程 第五章 常用UI界面控件
程序员文章站
2022-03-13 21:25:19
...
第五章 常用UI界面控件
除了前面介绍的TextView、EditView、Button、CheckBox和RadioGroup等控件以外,Android系统还提供了很多其他界面控件,例如Toast信息提示,对话框、下拉列表、选项卡、日期时间控件和菜单等
5.1信息提示和对话框
5.1.1Toast
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Toast示例\n"
/>
<Button android:id="@+id/btn_1"
android:text="显示短时的Toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:id="@+id/btn_2"
android:text="显示长时的Toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:id="@+id/btn_3"
android:text="在Toast中显示图片"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button Btn1,Btn2 ,Btn3 ;//实例化两个按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Btn1 =(Button)findViewById(R.id.btn_1 );
Btn2 = (Button)findViewById(R.id.btn_2);
Btn3 = (Button)findViewById(R.id.btn_3);
Btn1.setOnClickListener( new Button.OnClickListener(){ //侦听按钮1被按下的动作
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "短时的Toast信息", Toast.LENGTH_SHORT).show();
}
});
Btn2.setOnClickListener( new Button.OnClickListener(){ //侦听按钮1被按下的动作
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "长时的Toast信息", Toast. LENGTH_LONG).show();
}
});
Btn3.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Toast toast = new Toast(getApplicationContext()); //toast实例化
ImageView myview = new ImageView(getApplicationContext()); //实例化ImageView
myview.setImageResource(R.drawable.fw8005); //和指定的图片关联
toast.setView(myview); //将toast实例和图片实例关联
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); //定位图片显示位置,否则以默认位置显示
toast.show(); //显示toast
}
});
}
}
运行结果
5.1.2 状态栏提醒 Notification
与Toast不同,Notification是显示在状态栏的提示信息,痛呀它也不会打断用户当前的操作,而且它支持更复杂的点击事件响应
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//创建 NotificationManager对象,负责“发出”与“取消”Notification
//下面创建 Notification ,参数依次为icon的资源id、在状态栏上展示的滚动信息、时间。
final Notification notifyDetails = new Notification(R.drawable.image1,"这是Notification提示信息",System.currentTimeMillis());
Button btnstart = (Button)findViewById(R.id.btn_1);
Button btncancel = (Button)findViewById(R.id.btn_2);
Button btnfinish = (Button)findViewById(R.id.btn_3);
final TextView txt= (TextView)findViewById(R.id.tv_message);
btnstart.setOnClickListener(new OnClickListener() { //“显示提示”按钮对应的点击事件
public void onClick(View v) {
Context context = getApplicationContext();
CharSequence contentTitle = "您好";
CharSequence contentText = "这是给您的Notification提示";
txt.setText("Notification示例----您选中了显示提示\n");
Intent notifyIntent = new Intent(android.content.Intent.ACTION_VIEW);
//PendingIntent 为Intent的包装,这里是启动Intent的描述。
//PendingIntent.getActivity 返回的PendingIntent表示此PendingIntent实例中的Intent是用于启动 Activity 的Intent。
//PendingIntent.getActivity的参数依次为:Context,发送者的请求码(可以填0)、用于系统发送的Intent、标志位。
PendingIntent intent = PendingIntent.getActivity(MainActivity.this, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
//notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
}
});
btncancel.setOnClickListener(new OnClickListener() { //“关闭程序”按钮对应的点击事件
public void onClick(View v) {
txt.setText("Notification示例----您取消了显示提示\n");
mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
}
});
btnfinish.setOnClickListener(new OnClickListener() { //“关闭程序”按钮对应的点击事件
public void onClick(View v) {
finish();//退出应用程序
}
});
}
运行结果
5.1.3 带自动输入提示的文本框AutoCompleteTextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="AutoCompleteTextView示例\n"
/>
<AutoCompleteTextView
android:id="@+id/auto_complete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="aotuStrings">
<item>Bejing Railway Station</item>
<item>Beijing Station</item>
<item>北京西站</item>
<item>北京*</item>
<item>北京天气</item>
<item>北京天南小吃店</item>
<item>上海科技馆</item>
<item>上海科技大学</item>
<item>上海科技大学学报</item>
<item>上海大学</item>
<item>上海交通大学</item>
<item>上海理工大学</item>
<item>上海市人民*</item>
<item>上海科工委</item>
</string-array>
</resources>
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity {
// static final String[] AOTUSTR = new String[] {
// "Beijing Station" ,"Bejing Railway Station", "北京西站", "北京*", "北京天气", "北京天南小吃店"
// ,"上海科技馆","上海科技大学学报","上海科技大学", "上海交通大学", "上海理工大学", "上海市人民*"
// };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, getResources().getStringArray(R.array.aotuStrings));
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, R.array.aotuStrings);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.auto_complete);
textView.setAdapter(adapter);
}
}
运行结果:
5.1.4 提示对话框 AlterDialog
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnstart = (Button)findViewById(R.id.btn_1);
btnstart.setOnClickListener(new OnClickListener() { //按钮对应的点击事件
public void onClick(View v) {
AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity.this);//建AlertDialog.Builder对象
myDialog.setTitle( "提示 " );// 设置标题
myDialog.setMessage( "这是一个AlertDialog对话框 !" );// 设置显示消息
myDialog.setNegativeButton("取消", null);
myDialog.setPositiveButton("确定", null);
myDialog.show();// 显示
}
});
}
}
运行结果
5.1.5 进度条对话框 ProcessingDialog
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ProcessDialogActivity extends Activity {
private Button button1,button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.btn_1);
button2 = (Button) findViewById(R.id.btn_2);
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// 实例化一个ProgressDialog
ProgressDialog progressDialog = new ProgressDialog(ProcessDialogActivity.this);
progressDialog.setTitle("圆形进度对话框");
progressDialog.setMessage("正在下载中,请稍候......");
// 设置ProgressDialog的显示样式,ProgressDialog.STYLE_SPINNER代表的是圆形进度条
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
}
});
button2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// 实例化一个ProgressDialog
ProgressDialog progressDialog = new ProgressDialog(ProcessDialogActivity.this);
progressDialog.setTitle("水平进度对话框");
progressDialog.setMessage("正在下载中,请稍候......");
// 设置最大进度,ProgressDialog的进度范围是从1-10000
progressDialog.setMax(100);
// 设置主进度
//progressDialog.setProgress(50);
// 设置第二进度
//progressDialog.setSecondaryProgress(70);
// 设置ProgressDialog的显示样式,ProgressDialog.STYLE_HORIZONTAL代表的是水平进度条
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.incrementProgressBy(25);
progressDialog.show();
}
});
}
}
运行结果:
5.2 常容器类控件
5.2.1列表控件 ListView
/res/values/arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listStrings">
<item>北京</item>
<item>天津</item>
<item>上海</item>
<item>河北</item>
<item>河南</item>
<item>内蒙古</item>
<item>山东</item>
<item>山西</item>
<item>陕西</item>
</string-array>
</resources>
res/layout/list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:textSize="18sp"
android:textColor="#0000ff">
</TextView>
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ListView示例\n"
/>
<ListView
android:id="@+id/listview"
android:background = "#cccccc"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
// private String[] listItem = new String[]{"北京","天津","上海","河北","河南"};
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mylistview = (ListView)findViewById(R.id.listview);
//ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItem);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.list_item, getResources().getStringArray(R.array.listStrings));
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//下面的onItemClick()函数中一共有四个参数:
//参数0表示适配器控件(就是ListView);参数1表示适配器内部的控件(是ListView中的子项);
//参数2表示适配器内部的控件(就是子项的位置);参数3表示子项的行号
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//ListView listView = (ListView) parent;
String itemString=((TextView)view).getText().toString();
//获取点击项的文字
Toast.makeText(MainActivity.this, "您点击了列表项:"+itemString, Toast.LENGTH_LONG).show();
}
});
mylistview.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//ListView listView = (ListView) parent;
Toast.makeText(MainActivity.this, "您选择了列表项:"+((TextView)view).getText().toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
运行结果
5.2.2下拉列表 Spinner
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
private String[] listItem = new String[]{"北京","天津","上海","河北","河南"};
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner myspinner = (Spinner) findViewById(R.id.spinner1);
//这里采用系统默认样式 android.R.layout.simple_spinner_item
//ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listItem);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.list_item, listItem);
myspinner.setAdapter(adapter);
myspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner = (Spinner) parent;
Toast.makeText(MainActivity.this, "您选择了列表项:"+spinner.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
运行结果
5.2.3 选项卡
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/txt1"
android:background="#FF00FF"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="第一个标签页的区域" />
<EditText android:id="@+id/edit2"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第二个标签页的区域"
/>
<EditText android:id="@+id/edit3"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="第三个标签页区域"
android:textColor="#ffffFF"/>
</FrameLayout>
import android.os.Bundle;
import android.view.LayoutInflater;
import android.os.Build;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends TabActivity {//继承自TabActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();//调用TabActivity的getTabHost()方法获得TabHost对象
LayoutInflater.from(this).inflate(R.layout.activity_main,tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("Tab-1")
.setContent(R.id.txt1));
//通过TabHost创建Tab选项
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("Tab-2").
setContent(R.id.edit2));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("Tab-3")
.setContent(R.id.edit3));
}
}
运行结果
5.3 日期和时间控件
5.3.1 DatePicker 和 TimePicker
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="日期和时间控件的示例\n"
/>
<Button android:id="@+id/btn_1"
android:text="DatePicker示例"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:id="@+id/btn_2"
android:text="TimePicker示例"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
layout/date.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<DatePicker
android:id="@+id/datePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
layout/time.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TimePicker
android:id="@+id/timePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
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;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button)findViewById(R.id.btn_1);
btn1.setOnClickListener(new OnClickListener() { //按钮对应的点击事件
public void onClick(View v) {
Intent myintent = new Intent(MainActivity.this,DatePickerActivity.class);
startActivity(myintent);
}
});
Button btn2 = (Button)findViewById(R.id.btn_2);
btn2.setOnClickListener(new OnClickListener() { //按钮对应的点击事件
public void onClick(View v) {
Intent myintent = new Intent(MainActivity.this,TimePickerActivity.class);
startActivity(myintent);
}
});
}
}
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
public class DatePickerActivity extends Activity {
private DatePicker mydatePicker;
private TextView textDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date);
this.setTitle("日期控件的示例");
textDate = (TextView) findViewById(R.id.textDate);
mydatePicker= (DatePicker)findViewById(R.id.datePicker);
Calendar calendar = Calendar.getInstance(Locale.CHINA);
int year = calendar.get(Calendar.YEAR);
int monthOfYear = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
mydatePicker.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
textDate.setText("\n您选择的日期是: " + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
}
});
}
}
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
public class TimePickerActivity extends Activity {
private TimePicker mytimePicker;
private TextView textTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.time);
this.setTitle("时间控件的示例");
textTime = (TextView) findViewById(R.id.textTime);
mytimePicker = (TimePicker)findViewById(R.id.timePicker);
Calendar calendar = Calendar.getInstance(Locale.CHINA);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
mytimePicker.setCurrentHour(hour);
mytimePicker.setCurrentMinute(minute);
mytimePicker.setOnTimeChangedListener(new OnTimeChangedListener()
{
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
{
textTime.setText("您选择的时间是: " + hourOfDay + "时" + minute + "分");
}
});
}
}
运行结果
5.3.2 DatePickerDialog和 TimePikerDialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="DatePickerDialog和TimePickerDialog示例"
/>
<Button android:id="@+id/btn_1"
android:text="选择日期对话框"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:id="@+id/btn_2"
android:text="选择时间对话框"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setTitle("日期和时间选择对话框示例");
Button btn1 = (Button)findViewById(R.id.btn_1);
btn1.setOnClickListener(new OnClickListener() { //按钮对应的点击事件
public void onClick(View v) {
/**
* 实例化一个DatePickerDialog的对象
* 第二个参数是一个DatePickerDialog.OnDateSetListener匿名内部类,当用户选择好日期点击done会调用里面的onDateSet方法
*/
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth)
{
Toast.makeText(getApplicationContext(), "日期:" + year + "-" + (monthOfYear + 1) + "-" + dayOfMonth, Toast.LENGTH_SHORT).show();
}
}, 2014, 8, 7);
datePickerDialog.show();
}
});
Button btn2 = (Button)findViewById(R.id.btn_2);
btn2.setOnClickListener(new OnClickListener() { //按钮对应的点击事件
public void onClick(View v) {
// 构建一个 TimePickerDialog 并显示
/**
* 实例化一个TimePickerDialog的对象
* 第二个参数是一个TimePickerDialog.OnTimeSetListener匿名内部类,当用户选择好时间后点击done会调用里面的onTimeset方法
*/
TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
Toast.makeText(getApplicationContext(), "Time: " + hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();
}
}, 8, 15, true);
timePickerDialog.show();
}
});
}
}
运行结果
5.3.3 AnalogClock 和 DigitalClock
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="数字时钟:"
/>
<DigitalClock
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="\n模拟时钟:"
/>
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setTitle("时钟示例");
}
}
运行结果
5.4菜单
5.4.1选项菜单 OptionMenu
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//分别用三种方法添加菜单项
// 1.直接指定标题,ID号=0
menu.add("菜单项1");
// 2.通过资源指定标题
menu.add(1, 1, 3,R.string.MenuItem1);
// 3.指定菜单项的组号、ID、排序号、标题
menu.add(1, 2, 3, "菜单项3");
//menu.add(1, 3, 4, "菜单项4");
//menu.add(1, 4, 5, "菜单项5");
//menu.add(1, 5, 6, "菜单项6");
//menu.add(1, 6, 7, "菜单项7");
//menu.add(1, 7, 8, "菜单项8");
//menu.add(1, 8, 8, "菜单项9");
//menu.add(1, 9, 8, "菜单项10");
//menu.add(1, 10, 8, "菜单项11");
// 如果希望显示菜单,请返回true
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
Toast.makeText(MainActivity.this, "您点击了菜单项1", Toast.LENGTH_LONG).show();
return true;
case 1:
Toast.makeText(MainActivity.this, "您点击了菜单项2", Toast.LENGTH_LONG).show();
return true;
case 2:
Toast.makeText(MainActivity.this, "您点击了菜单项3", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
运行结果
5.4.2 子菜单SubMenu
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu submenu1 = menu.addSubMenu(1, 0, 0,"菜单项1");//增添
SubMenu submenu2 = menu.addSubMenu(2, 1, 0,"菜单项2");
SubMenu submenu3 = menu.addSubMenu(3, 2, 0,"菜单项3");
submenu1.add(1, 3, 0, "子菜单项1");//增添
submenu1.add(1, 4, 1, "子菜单项2");
submenu2.add(2, 5, 0, "子菜单项3");
submenu2.add(2, 6, 1, "子菜单项4");
submenu3.add(3, 7, 0, "子菜单项5");
submenu3.add(4, 8, 1, "子菜单项6");
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 3:
Toast.makeText(MainActivity.this, "您点击了子菜单项1", Toast.LENGTH_LONG).show();
return true;
case 4:
Toast.makeText(MainActivity.this, "您点击了子菜单项2", Toast.LENGTH_LONG).show();
return true;
case 5:
Toast.makeText(MainActivity.this, "您点击了子菜单项3", Toast.LENGTH_LONG).show();
return true;
case 6:
Toast.makeText(MainActivity.this, "您点击了子菜单项4", Toast.LENGTH_LONG).show();
return true;
case 7:
Toast.makeText(MainActivity.this, "您点击了子菜单项5", Toast.LENGTH_LONG).show();
return true;
case 8:
Toast.makeText(MainActivity.this, "您点击了子菜单项6", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
运行结果
5.4.3上下文菜单ContextMenu
推荐阅读
-
Android入门教程之Android中UI控件总结
-
Android常用UI控件的使用
-
Android studio APP开发 第五章 常用控件讲解
-
【从零到一】Android UI界面(二) | 简单控件篇
-
界面控件Telerik UI for WinForms入门教程 - Telerik Upgrade API Analyzer Windows Formstelerikc#.net 6.0
-
WPF应用界面开发第三方控件Telerik UI for WPF教程 - 如何更改控件外观 telerikWPFUIc#
-
界面控件Telerik UI for WinForm初级教程 - 最新的内部构建 TelerikWinFormC#
-
界面控件Telerik UI for WinForm初级教程 - 开始下载产品文件 Telerikwinformc#.net
-
MindFusion Report教程:创建嵌套DataRanges UI界面报表控件报表
-
【Android】第5章(1)常用基本控件--本章示例主界面