MainActivity.java
public class MainActivity extends Activity {
private Button btnDlg=null;
private String []data =null;
private Button btnDatePicker=null;
private Button btnTimePicker=null;
private Button btnContextMenu=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDlg=(Button) findViewById(R.id.btn_dlg);
data=getResources().getStringArray(R.array.data);
btnDlg.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("對(duì)話框");
builder.setItems(data,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyToast.makeText(getApplicationContext(),"這里的which表示選項(xiàng)位置"+which, Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyToast.makeText(getApplicationContext(),"這里的which表示按鈕位置"+which, Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("醬油",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyToast.makeText(getApplicationContext(),"這里的which表示按鈕位置"+which, Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyToast.makeText(getApplicationContext(),"這里的which表示按鈕位置"+which, Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}
});
btnDatePicker=(Button) findViewById(R.id.btn_date_picker);
btnDatePicker.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DatePickerDialog dpd=new DatePickerDialog(MainActivity.this, new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
MyToast.makeText(getApplicationContext(), year+"-"+(monthOfYear+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},2012, 11, 22);
dpd.show();
}
});
btnTimePicker=(Button) findViewById(R.id.btn_time_picker);
btnTimePicker.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TimePickerDialog tpd=new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
MyToast.makeText(getApplicationContext(), hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
}
},0, 0, true);
tpd.show();
}
});
btnContextMenu=(Button) findViewById(R.id.btn_context_menu);
btnContextMenu.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MyToast.makeText(getApplicationContext(),"這是點(diǎn)擊", Toast.LENGTH_SHORT).show();
}
});
btnContextMenu.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
MyToast.makeText(getApplicationContext(),"這是長(zhǎng)按", Toast.LENGTH_SHORT).show();
return false;
}
});
registerForContextMenu(btnContextMenu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "play");
menu.add(0, 1, 1, "pause");
SubMenu submenu=menu.addSubMenu(1, 2, 2, "system");
submenu.add(1, 3, 1, "phone");
submenu.add(1, 4, 2, "exit");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 4:
android.os.Process.killProcess(android.os.Process.myPid());
break;
default:
MyToast.makeText(getApplicationContext(),""+item.getTitle(), Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_exit:
android.os.Process.killProcess(android.os.Process.myPid());
break;
default:
MyToast.makeText(getApplicationContext(),"contexMenu"+item.getTitle(), Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.menu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
}
MyToast.java
public class MyToast {
public static Toast makeText(Context context,String text,int duration){
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view= inflater.inflate(R.layout.custom_toast_layout,null);
ImageView image= (ImageView) view.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
TextView textView= (TextView) view.findViewById(R.id.text);
textView.setText(text);
Toast t= new Toast(context);
t.setGravity(Gravity.CENTER_VERTICAL,0, 0);
t.setDuration(duration);
t.setView(view);
return t;
}
}
custom_toast_layout.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000" > <ImageViewandroid:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent" /> <TextViewandroid:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textSize="20sp"
android:textColor="#000000" /></LinearLayout>
activity_main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/btn_dlg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/btn_dlg" />
<Button
android:id="@+id/btn_date_picker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/btn_date_picker" /> <Button
android:id="@+id/btn_time_picker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/btn_time_picker" /> <Button
android:id="@+id/btn_context_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/btn_context_menu" /></LinearLayout>
string.xml
<resources> <stringname="app_name">12-04</string> <stringname="hello_world">Hello world!</string> <stringname="menu_settings">Settings</string> <stringname="title_activity_main">MainActivity</string> <stringname="btn_dlg">對(duì)話框</string> <string-arrayname="data"> <item>選項(xiàng)零</item> <item>選項(xiàng)一</item> <item>選項(xiàng)二</item> <item>選項(xiàng)三</item> <item>選項(xiàng)四</item> <item>選項(xiàng)五</item> <item>選項(xiàng)六</item> </string-array> <stringname="btn_date_picker">DatePicker</string> <stringname="btn_time_picker">TimePicker</string> <stringname="btn_context_menu">ContextMenu</string></resources>
menu.xml
<menuxmlns:android="http://schemas.android.com/apk/res/android" > <item
android:id="@+id/menu_play"
android:title="play"/> <item
android:id="@+id/menu_pause"
android:title="pause"/> <item
android:id="@+id/menu_system"
android:title="system"> <menu> <item
android:id="@+id/menu_sms"
android:title="pause"/> <item
android:id="@+id/menu_phone"
android:title="phone"/> <item
android:id="@+id/menu_exit"
android:title="exit"/>
</menu> </item></menu>
網(wǎng)站題目:Menu,CustomToast-創(chuàng)新互聯(lián)
當(dāng)前URL:http://www.rwnh.cn/article40/ddciho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站收錄、網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容