网站建设资讯

NEWS

网站建设资讯

Menu,CustomToast-创新互联

目前创新互联已为千余家的企业提供了网站建设、域名、网站空间、网站托管、服务器租用、企业网站设计、西安网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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("对话框");
                builder.setItems(data,new DialogInterface.OnClickListener() {
                    
public void onClick(DialogInterface dialog, int which) {
                        MyToast.makeText(getApplicationContext(),"这里的which表示选项位置"+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(),"这是点击", Toast.LENGTH_SHORT).show();
            }
        });
        btnContextMenu.setOnLongClickListener(new OnLongClickListener() {
            
public boolean onLongClick(View v) {
                MyToast.makeText(getApplicationContext(),"这是长按", 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

    

activity_main.xml

  

string.xml

  12-04  Hello world!  Settings  MainActivity  对话框     选项零   选项一   选项二   选项三   选项四   选项五   选项六    DatePicker  TimePicker  ContextMenu

menu.xml

                        
     

网站题目:Menu,CustomToast-创新互联
浏览路径:http://njwzjz.com/article/ddciho.html