• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

绝望生鱼片

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

Android学习笔记——Day3

7)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="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="请输入你的查询条件:" />

    <AutoCompleteTextView android:id="@+id/AutoCompleteTextView01"
        android:layout_width="fill_parent" android:layout_height="wrap_content"></AutoCompleteTextView>
</LinearLayout>

public class AutoTextFrame extends Activity {
    private String[] datas = {"abc","ade","bcd","bkff","akhlv"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.autocompletetextui);
        AutoCompleteTextView autoText = (AutoCompleteTextView)this.findViewById(R.id.act_text);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,datas);
        autoText.setAdapter(adapter);
    }
}

8)日期选择器:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/ScrollView01"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout 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="请选择你的出生年月日:" />
        <DatePicker android:id="@+id/DatePicker01"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></DatePicker>
        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:text="请选择你的出生时间:" />
        <TimePicker android:id="@+id/TimePicker01"
            android:layout_width="wrap_content" android:layout_height="wrap_content">
        </TimePicker>
        <Button android:text="确定" android:id="@+id/Button01"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>
</ScrollView>

public class ShowView extends Activity {
     private DatePicker picker;
    private TimePicker tp;
    private Button button;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        picker = (DatePicker)this.findViewById(R.id.DatePicker01);
        tp = (TimePicker)this.findViewById(R.id.TimePicker01);
        button = (Button)this.findViewById(R.id.Button01);
        picker.init(1982,6,12,null);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int year = picker.getYear();
                int m = picker.getMonth();
                int d = picker.getDayOfMonth();
                int h = tp.getCurrentHour();
                int s = tp.getCurrentMinute();
                setTitle("你的出生年月日为: "+year+"-"+m+"-"+d+"   "+h+":"+s);
               
            }
        });
    }
}


9)图片、图片按钮:
<?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="学习ImageView以及ImageButton的使用" />

    <ImageView android:id="@+id/ImageView01"
        android:layout_width="wrap_content" android:src="@drawable/map"
        android:layout_height="wrap_content"></ImageView>
    <ImageButton android:id="@+id/ImageButton01" android:src="@drawable/lostfocusimage"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageButton>
</LinearLayout>

public class ShowView extends Activity {
    /** Called when the activity is first created. */
    private ImageButton button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (ImageButton)this.findViewById(R.id.ImageButton01);
        button.setOnClickListener(new OnClickListener() {
           
            public void onClick(View v) {
                // TODO Auto-generated method stub
                setTitle("按了按钮");
            }
        });
    }
}


10)tab面板:
<?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对应一个Tab的内容 -->
    <TextView android:text="这是Tab1的内容" android:id="@+id/TextView01"
        android:layout_width="fill_parent" android:layout_height="fill_parent"></TextView>
    <TextView android:text="这是Tab2的内容" android:id="@+id/TextView02"
        android:layout_width="fill_parent" android:layout_height="fill_parent"></TextView>
    <TextView android:text="这是Tab3的内容" android:id="@+id/TextView03"
        android:layout_width="fill_parent" android:layout_height="fill_parent"></TextView>
    <ImageView android:id="@+id/ImageView01" android:src="@drawable/map"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>

public class ShowImageSwitch extends TabActivity{
    /** Called when the activity is first created. */
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        //setContentView(R.layout.main);
        // 获取TabHost
        TabHost tabHost = this.getTabHost();
        LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(),true);
        // 最重要的代码是下面
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.TextView01));
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.TextView02));
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3").setContent(R.id.TextView03));
               
    }
   
}

Toast t = Toast.makeText(this,"你已经登录到该系统了...",Toast.LENGTH_LONG);
t.show();

不同的Activity传递数据:
A、First Activity
 Intent i = new Intent(this,ShowFrame.class);
 Bundle b = new Bundle();
 b.putString("shengao",shengao);
 b.putString("sex",sex);
 i.putExtras(b);
 startActivityForResult(i,1);
B、Second Activity
Bundle b = this.getIntent().getExtras();
String shengao = b.getString("shengao");
String sex = b.getString("sex");

返回上一个Activity:
A、在第一个Activity调用startActivityForResult(..,..),如:
public void onClick(View v) {
    ...
        Intent i = new Intent(this,ShowFrame.class);
        Bundle b = new Bundle();
        b.putString("shengao",shengao);
        b.putString("sex",sex);      
        i.putExtras(b);      
        startActivityForResult(i,1);
}
//该方法是第二个活动Activity单返回时被调用,requestCode是之前设置的值
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1){
            switch(resultCode){
                case RESULT_OK:{
                    Bundle b = data.getExtras();
                    String shengao = b.getString("shengao");
                    String sex = b.getString("sex");
                    if(sex.equals("男")){
                        rb_1.setChecked(true);
                    }else{
                        rb_2.setChecked(true);
                    }
                    String tizhong = b.getString("tizhong");
                   
                    setTitle("你的标准体重是:"+tizhong);
                    et.setText(shengao);
                    break;
                }
               
            }
        }
}

B、在第二个Activity处理完数据之后,要消毁该屏幕对象
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    this.setResult(RESULT_OK,this.getIntent());
    this.finish();
}


常用组件之二:
11) Menu 以及MenuItem
A、利用代码来增加菜单项:
<?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="@string/hello" />
</LinearLayout>

public class MenuView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    // 当客户按下手机的menu功能的时候会回调oncreateOptionsMenu方法
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        /*
    * MenuItem add(int groupId,int itemId,int order,int titleRes):
    * 参数说明:
    * 1)groupId:指菜单项所属的组id
    * 2)itemId:设定菜单项的id,唯一标识该菜单项
     * 3)order:设定菜单项在菜单中的位置
    * 4)titleRes:设定菜单项上的内容
    */
        menu.add(0,1,1,"添加").setIcon(R.drawable.ic_menu_agenda);
        menu.add(0,2,2,"离开").setIcon(R.drawable.ic_menu_close_clear_cancel);
        return super.onCreateOptionsMenu(menu);
    }
    // 定义menu响应单击事件
    /*
     * boolean onMenuItemSelected(int featureId, MenuItem item)
     * featureId - 菜单所在的面板的id
       item - 被选中的菜单项.
     */
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId()){
            case 1:{
                setTitle("选择了添加");
                break;
            }
            case 2:{
                setTitle("选择了离开");
                break;
            }
        }
        return super.onOptionsItemSelected(item);
    }
}

增加子菜单:
public boolean onCreateOptionsMenu(Menu menu) { 
    boolean result = super.onCreateOptionsMenu(menu);  
    SubMenu fileMenu = menu.addSubMenu("File"); 
    SubMenu editMenu = menu.addSubMenu("Edit"); 
    fileMenu.add("new"); 
    fileMenu.add("open"); 
    fileMenu.add("save"); 
    editMenu.add("undo"); 
    editMenu.add("redo");  
    return result;
}

B、利用xml文件来增加菜单项
i、在res新建一个menu目录,再建menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item01" android:title="你选择了增加"></item>
    <item android:id="@+id/item02" android:title="你选择了离开"></item>
</menu>
ii、public class MenuView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    //从menu.xml中读取菜单项
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       
        switch(item.getItemId()){
            case 1:{
                setTitle("选择了添加");
                break;
            }
            case 2:{
                setTitle("选择了离开");
                break;
            }
        }
        return true;
    }
}

//PreferencesActivity是Android 中专门用来实现程序设置界面及参数存储的一个Activity
/*PreferencesActivity常见的几个组件:
 * PreferenceScreen:设置页面,可嵌套形成二级设置页面,用Title参数设置标题。
 * PreferenceCategory:某一类相关的设置,可用Title参数设置标题。
 * CheckBoxPreference:是一个CheckBox设置,只有两种值,true或false,
 * 可用Title参数设置标题,用summaryOn和summaryOff参数来设置控件选中和未选中时的提示。
 */
public class Settings extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
         * The addPreferencesFromResource( ) method reads the settings
         * definition from XML and inflates it into views in the current
         * activity. All the heavy lifting takes place in the PreferenceActivity
         * class.
         */
        addPreferencesFromResource(R.xml.settings);
    }

}
res/xml/settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference android:title="@string/music_title"
        android:key="music" android:summary="@string/music_summary" android:defaultValue="true"></CheckBoxPreference>
    <CheckBoxPreference android:title="@string/hints_title"
        android:key="hints" android:summary="@string/hints_summary" android:defaultValue="true"></CheckBoxPreference>
</PreferenceScreen>

12)、进度条、拖动条、星级:
A、进度条分类
长形进度条 (progressBarStyleHorizontal)
大圆形进度条(progressBarStyleLarge)
小圆形进度条 (progressBarStyleSmall)
默认风格 (progressBarStyle)
<?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="@string/hello"
/>
<ProgressBar android:id="@+id/ProgressBar01" android:layout_width="200dp"
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content"
android:indeterminate="false"
android:visibility="gone"
></ProgressBar>
<ProgressBar android:id="@+id/ProgressBar02" android:layout_width="wrap_content"
android:max="100"
style="?android:attr/progressBarStyleLarge"
android:progress="50" android:secondaryProgress="70"
android:indeterminate="false"
android:visibility="gone"
android:layout_height="wrap_content"></ProgressBar>
<Button android:text="开始" android:id="@+id/Button01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>


清单中,ProgressBar01为长形进度条,ProgressBar02为大圆形进度条,使用方法为声明“style=”在样式上加上各自属性,加载即可。

进度条主要属性方法
    indeterminate
进度条分 不确定 (indeterminate=true)和 确定 (indeterminate=false)2种,默认值是(indeterminate=true)不确定
    setMax
设置进度条的最大值,同时,确定(indeterminate=false)进度条中的最大值的设定,将调用 setMax()方法。
    setProgress
Android 进度条中当前进度值的设置。
    setSecondaryProgress
第二进度条的设置。
    setVisibility
设置可见性。


<?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="圆形进度条" />
    <ProgressBar android:id="@+id/ProgressBar01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ProgressBar>
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="水平进度条" />
    <ProgressBar android:id="@+id/ProgressBar02"
        android:layout_width="200dip" style="?android:attr/progressBarStyleHorizontal"
        android:max="100" android:progress="20" android:secondaryProgress="75"
        android:layout_height="wrap_content"></ProgressBar>
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="你选择的进度是" />

    <SeekBar android:id="@+id/SeekBar01" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:max="100"
        android:progress="30"></SeekBar>
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="星级" />

    <RatingBar android:id="@+id/RatingBar01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></RatingBar>
</LinearLayout>

拖动条:SeekBar
<SeekBar android:layout_height="wrap_content"
            android:layout_width="150px" android:max="100" android:id="@+id/sbMusic"></SeekBar>
//为拖条增加事件
musicSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            //用户停止拖动拖动时被调用...
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast t = Toast.makeText(ProgreeBarUI.this,"用户停止拖动拖动条...",Toast.LENGTH_SHORT);
                t.show();
               
            }
            //用户开始拖动拖动时被调用...
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast t = Toast.makeText(ProgreeBarUI.this,"用户准备拖动拖动条...",Toast.LENGTH_SHORT);
                t.show();
               
            }
            //用户拖动拖动过程中,拖动条的时改变时被调用...
            /*
             * 方法三个参数含义:
             * seekBar - 目标拖动条
                progress - 拖动条的当前进度
                fromUser - True if the progress change was initiated by the user.

             */
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                seekBarText.setText("拖动条当前进度:"+progress+"%");
            }
});


星级进度条:
<RatingBar android:id="@+id/RatingBar01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:rating="1" android:numStars="3"></RatingBar>

public class ShowView extends Activity {
    /** Called when the activity is first created. */
    // 得到评级条
    private RatingBar tbar;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tbar = (RatingBar)this.findViewById(R.id.RatingBar01);
        tbar.setRating(3.5f);// 这是用来设置评分等级的
    }
}


 

posted on 2011-07-06 16:50  绝望生鱼片  阅读(1205)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3