第五章 基于视图的用户界面设计

一. DatePiceker视图

类似与TimePicker视图,使用DatePiceker视图可以让用户在Activity中选择一个特定的日期

**使用DatePicker视图:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <Button android:id="@+id/btnSet"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="I am all set!"

        android:onClick="onClick" />

     <DatePiceker android:id=@+id/datePicker"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"/>


    <TimePicker android:id="@+id/timePicker"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

</LinearLayout>


package com.jfdimarzio.basicviews4;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.TimePicker;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    TimePicker timePicker;

    DatePicker datePicker;


     int hour,minute;


    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        timePicker = (TimePicker) findViewById(R.id.timePicker);

        timePicker.setIs24HourView(true);

        datePicker=(DatePicker)findViewById(R.id,datePicker);

    }


    private TimePickerDialog.onTimeSetListener mTimeSetListener =

        new TimePickerDialog.onTimeSetListener(){

             public void onTimeSet(TimePicker view,int hourOfDay,int minuteOfHour){

                   hour=hourOfDay;

                   minute=minuteOfHour;

                   SimpleDateFormat timeFormat.format(date);


                   Toast.makeText(getBaseContext(),

                        "you have selected" + strDate,Toast.LENGTH_SHORT).show();

             }

        };

    public void onClick(View view) {

        Toast.makeText(getBaseContext(),

                "Date selected:" +

                        (datePicker.getMonth()+1) +

                        "/" + datePicker.getDayOfMonth()+

                        "/" + datePicker.getYear()+"\n"+

                        "Time selected:"+timePicker.getHour()+

                        ":"+timePicker.getMinute(),

                Toast.LENGTH_SHORT).show();

    }


}


二. 使用列表视图显示长列表

Android中有2种列表视图:ListView SpinnerView

1.      ListView视图

è定制ListView


部分代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >


    <Button

        android:id="@+id/btn"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Show selected items"

        android:onClick="onClick"/>

    <ListView

        android:id="@+id/android:list"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

</LinearLayout>


package com.jfdimarzio.basicviews5;


import android.app.ListActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;


public class MainActivity extends ListActivity {

    String[] presidents;


    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);

        ListView lstView = getListView();

        lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        lstView.setTextFilterEnabled(true);

        presidents =

                getResources().getStringArray(R.array.presidents_array);

        setListAdapter(new ArrayAdapter<String>(this,

                android.R.layout.simple_list_item_checked, presidents));

    }


    public void onListItemClick(

            ListView parent, View v, int position, long id)

    {

        Toast.makeText(this,

                "You have selected " + presidents[position],

                Toast.LENGTH_SHORT).show();

    }


    public void onClick(View view) {

        ListView lstView = getListView();


        String itemsSelected = "Selected items: \n";

        for (int i=0; i<lstView.getCount(); i++) {

            if (lstView.isItemChecked(i)) {

                itemsSelected += lstView.getItemAtPosition(i) + "\n";

            }

        }

        Toast.makeText(this, itemsSelected, Toast.LENGTH_LONG).show();

    }

}


2. SpinnerView视图

      什么时候用该视图?

ListView在Activity种显示一个长条目,但也许我们希望用户界面显示其他视图,这意味着没有额外的空间给全屏视图。此时需要用SpinnerView视图


部分代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <Spinner

        android:id="@+id/spinner1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:drawSelectorOnTop="true" />

</LinearLayout>



package com.jfdimarzio.basicviews6;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    String[] presidents;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        presidents =

                getResources().getStringArray(R.array.presidents_array);

        Spinner s1 = (Spinner) findViewById(R.id.spinner1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

                android.R.layout.simple_list_item_single_choice, presidents);

        s1.setAdapter(adapter);

        s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()

        {

            @Override

            public void onItemSelected(AdapterView<?> arg0,

                                       View arg1, int arg2, long arg3)

            {

                int index = arg0.getSelectedItemPosition();

                Toast.makeText(getBaseContext(),

                        "You have selected item : " + presidents[index],

                        Toast.LENGTH_SHORT).show();

            }

            @Override

            public void onNothingSelected(AdapterView<?> arg0) { }

        });

    }

}



三. 理解专用fragment

1. 使用ListFragment

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="horizontal" >

    <fragment

        android:name="com.jfdimarzio.listfragmentexample.Fragment1"

        android:id="@+id/fragment1"

        android:layout_weight="0.5"

        android:layout_width="0dp"

        android:layout_height="200dp" />

    <fragment

        android:name="com.jfdimarzio.listfragmentexample.Fragment1"

        android:id="@+id/fragment2"

        android:layout_weight="0.5"

        android:layout_width="0dp"

        android:layout_height="300dp" />

</LinearLayout>


<?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">

    <ListView android:id="@id/android:list"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_weight="1"

        android:drawSelectorOnTop="false"/>

</LinearLayout>

package com.jfdimarzio.listfragmentexample;


import android.app.ListFragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;


public class Fragment1 extends ListFragment {

    String[] presidents = {

            "Dwight D. Eisenhower",

            "John F. Kennedy",

            "Lyndon B. Johnson",

            "Richard Nixon",

            "Gerald Ford",

            "Jimmy Carter",

            "Ronald Reagan",

            "George H. W. Bush",

            "Bill Clinton",

            "George W. Bush",

            "Barack Obama"

    };

    @Override

    public View onCreateView(LayoutInflater inflater,

                             ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment1, container, false);

    }

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(getActivity(),

                android.R.layout.simple_list_item_1, presidents));

    }


    public void onListItemClick(ListView parent, View v,

                                int position, long id)

    {

        Toast.makeText(getActivity(),

                "You have selected " + presidents[position],

                Toast.LENGTH_SHORT).show();

    }

}


package com.jfdimarzio.listfragmentexample;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;


public class MainActivity extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

}


2.      使用使用DialogFragment

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.jfdimarzio.dialogfragmentexample.MainActivity">


    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Hello World!" />

</RelativeLayout>


package com.jfdimarzio.dialogfragmentexample;


import android.app.AlertDialog;

import android.app.Dialog;

import android.app.DialogFragment;

import android.content.DialogInterface;

import android.os.Bundle;


public class Fragment1 extends DialogFragment {

    static Fragment1 newInstance(String title) {

        Fragment1 fragment = new Fragment1();

        Bundle args = new Bundle();

        args.putString("title", title);

        fragment.setArguments(args);

        return fragment;

    }

    @Override

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        String title = getArguments().getString("title");

        return new AlertDialog.Builder(getActivity())

                .setIcon(R.mipmap.ic_launcher)

                .setTitle(title)

                .setPositiveButton("OK",

                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,

                                                int whichButton) {

                                ((MainActivity)

                                        getActivity()).doPositiveClick();

                            }

                        })

                .setNegativeButton("Cancel",

                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,

                                                int whichButton) {

                                ((MainActivity)

                                        getActivity()).doNegativeClick();

                            }

                        }).create();

    }

}


package com.jfdimarzio.dialogfragmentexample;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;


public class MainActivity extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Fragment1 dialogFragment = Fragment1.newInstance(

                "Are you sure you want to do this?");

        dialogFragment.show(getFragmentManager(), "dialog");

    }


    public void doPositiveClick() {

        //---perform steps when user clicks on OK---

        Log.d("DialogFragmentExample", "User clicks on OK");

    }

    public void doNegativeClick() {

        //---perform steps when user clicks on Cancel---

        Log.d("DialogFragmentExample", "User clicks on Cancel");

    }

}



3. 使用PreferenceFragment

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.jfdimarzio.preferencefragmentexample.MainActivity">



</RelativeLayout>


package com.jfdimarzio.preferencefragmentexample;


import android.os.Bundle;

import android.preference.PreferenceFragment;

public class Fragment1 extends PreferenceFragment {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //---load the preferences from an XML file---

        addPreferencesFromResource(R.xml.preferences);

    }

}


package com.jfdimarzio.preferencefragmentexample;


import android.app.FragmentManager;

import android.app.FragmentTransaction;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;


public class MainActivity extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        FragmentManager fragmentManager = getFragmentManager();

        FragmentTransaction fragmentTransaction =

                fragmentManager.beginTransaction();

        Fragment1 fragment1 = new Fragment1();

        fragmentTransaction.replace(android.R.id.content, fragment1);

        fragmentTransaction.addToBackStack(null);

        fragmentTransaction.commit();


    }

}

#设计#
全部评论

相关推荐

牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
06-25 09:33
厦门大学 Java
球球别拷打俺了:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司10个岗位
点赞 评论 收藏
分享
07-25 10:31
门头沟学院 Java
求问各位大佬,笔试都考点啥
投递科大讯飞等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务