android学习小结3-各种控件使用方式DEMO

    技术2024-06-27  68

    1、在1。5的android中,可以指定edittext中只输入数字,比如可以这样,就弹出小键盘了:android:inputType="numberDecimal",这样还可以接受输入包含小数点的数字了。

    2 读取资源文件中的内容   result.setText(getText(R.String.bmi_result));注意使用getText函数,读取资源文件中的内容.

    3 做一个象”关于我们“的对话框:    new AlertDialog.Builder(Bmi.this)            .setTitle(R.string.about_title)            .setMessage(R.string.about_msg)            .setPositiveButton("确认",       new DialogInterface.OnClickListener(){           public void onClick(               DialogInterface dialoginterface, int i){               }            })            .show();

    4 toast组件:显示短的提示消息,过几秒之后就消失:    Toast.makeText(Bmi.this, "关于我们", Toast.LENGTH_SHORT).show();5 url的打开   new AlertDialog.Builder(Bmi.this)    .setNegativeButton(R.string.homepage_label,            new DialogInterface.OnClickListener(){            public void onClick(                DialogInterface dialoginterface, int i){                //go to url                Uri uri = Uri.parse("http://sites.google.com/site/gasodroid/");                Intent intent = new Intent(Intent.ACTION_VIEW, uri);                startActivity(intent);            }     })     .show();   也可以把URI写到资源文件中去,   Uri uri=uri.parase(getString(R.string.homepage_uri));

    6 menu菜单   public boolean onCreateOptionsMenu(Menu menu) {  // TODO Auto-generated method stub     menu.add(0, MENU_ABOUT, 0, "关于").setIcon(R.drawable.help_browser);     menu.add(0, MENU_Quit, 0, "结束").setIcon(R.drawable.emblem_unreadable);  return super.onCreateOptionsMenu(menu); }    处理menu的点击动作public boolean onOptionsItemSelected(MenuItem item) {  // TODO Auto-generated method stub  switch(item.getItemId()) {        case MENU_ABOUT:             openOptionsDialog();             break;        case MENU_Quit:             finish();             break;  }  return super.onOptionsItemSelected(item); }7   <activity android:name=".Bmi"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>、  这里指出了启动的类是Bmi这个类,,<intent-filter>讲明了这个程序的性质,其中  <action android:name="android.intent.action.MAIN" />讲明了这个BMI是程序的切入点,<category android:name="android.intent.category.LAUNCHER" />讲明这个出现在程序的lanucher列表中

    8 intent之间传递数据    Intent intent = new Intent();                intent.setClass(Bmi.this, Report.class);                Bundle bundle = new Bundle();                bundle.putString("KEY_HEIGHT", field_height.getText().toString());                bundle.putString("KEY_WEIGHT", field_weight.getText().toString());                intent.putExtras(bundle);                startActivity(intent);接收信息:    Bundle bunde = this.getIntent().getExtras();        double height = Double.parseDouble(bunde.getString("KEY_HEIGHT"))/100;9 使用状态拦信息    import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;  NotificationManager barManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);          Notification barMsg = new Notification(       R.drawable.icon_128,           "HI",          System.currentTimeMillis()          );

         barMsg.defaults |= Notification.DEFAULT_SOUND;     //barMsg.defaults |= Notification.DEFAULT_ALL;          PendingIntent contentIntent = PendingIntent.getActivity(          this,          0,                new Intent(this, Bmi.class),                PendingIntent.FLAG_UPDATE_CURRENT);               barMsg.setLatestEventInfo(       Report.this,       "HI",                "OK",                contentIntent);          barManager.notify(0, barMsg);

    NotificationManager barManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);声明管理器,Notification barMsg = new Notification(       R.drawable.icon_128,           "HI",          System.currentTimeMillis()          );中声明提醒信息,System.currentTimeMillis()表示立刻显示;barMsg.setLatestEventInfo(       Report.this,       "HI",                "OK",                contentIntent);这里是添加状态栏的详细信息,这里的“HI”是显示的主题,第三个参数,这里的‘OK’是说明,contentIntent这个参数指明当点状态栏时,相应打开的intent.

    10 保存优先级的设置:   private void restorePrefs() {        SharedPreferences settings = getSharedPreferences(PREF, 0);        String pref_height = settings.getString(PREF_HEIGHT, "");      }  这里是找系统中是否以"BMI_PREF"字符串作为文件名的优先级设置的文件,有的话,以settings做为代号来操作当用户离开activity时,把值进行保存,重写onPause()函数: super.onPause();  // Save user preferences. use Editor object to make changes.        SharedPreferences settings = getSharedPreferences(PREF, 0);            settings.edit()                .putString(PREF_HEIGHT, field_height.getText().toString())                .commit();

    11 多语言界面并存:   比如原来的是英文,则res目录下放values,中文的话,在res目录下再放一个values-zh-rTW其中zh是主语系,-r是必须的,后面跟分支    Resources res = getResources();        Configuration conf = res.getConfiguration();        conf.locale = Locale.TRADITIONAL_CHINESE;        DisplayMetrics dm = res.getDisplayMetrics();        res.updateConfiguration(conf, dm);这里是强制使用中文界面,在程序中,针对不同语言提供不同的界面:  if (conf.locale==Local.TRADITIONAL_CHINESE)12 Spinner下拉菜单中的选择事件:

        field_feet.setOnItemSelectedListener(getFeet);  private Spinner.OnItemSelectedListener getFeet = new Spinner.OnItemSelectedListener() {        public void onItemSelected(AdapterView parent, View v, int position, long id) {               。。。。。。        }        public void onNothingSelected(AdapterView parent) {        }    };

     

    本文来自博客,转载请标明出处:http://blog.csdn.net/jackyrongvip/archive/2010/02/09/5303824.aspx

    最新回复(0)