开发者

Android: How to use Buttons in Preference Screen

I'd like to provide a Button in the PreferenceActivity. The user should be able to set a time (e.g. via TimePicker), but there's no ButtonPreference or something like that. I don't want to use EditTextPreference.

So do I have to use a default Button in the PreferenceActivity and save the se开发者_StackOverflow中文版ttings for it manually?

Regards,

cody


Create a custom DialogPreference that incorporates a TimePicker widget. No button required. Should be under 100 lines of code.

Here is a sample project showing, among other things, a custom ColorPreference.


Create a different layout including your custom controls and include ListView on top, see the example below

// extra_settings.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
    <ListView android:id="@android:id/list"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" android:layout_weight="1"/>
    <TextView
            android:id="@+id/feedback"
            android:text="Feedback"
            />
    <Button
            android:id="@+id/about"
            android:text="About"
            />
</LinearLayout>

Once this done, onCreate method of your class which is extended from PreferenceActivity add following line

setContentView(R.layout.settings_extras); //name of your layout


bind OnClickListener

View.OnClickListener feedbackPageRedirect = new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Feedback  ");

                startActivity(emailIntent);
            }
        };


You can put the button in the activity_layout.xml . For example, if "Activity_Setup.java" is your setup activity, "activity_setup.xml" is its layout, and "myprefs.xml" is the file where your preference layout is stored, then

your should put the button in "activity_setup.xml" under the list, most preferable in a TableRow, like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/llprefs">

<ListView
    android:id="@+id/android:list"
    android:tag="lvprefs"
    android:layout_width="match_parent"
    android:layout_height="0.0dp"
    android:layout_weight="1" >
</ListView>

    <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
     >

    <Button
        android:id="@+id/btRegister"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="@string/IdonothaveA"
        android:onClick="onClick"
         />

    <Button
        android:id="@+id/btForgot"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="@string/Iforgot"
        android:onClick="onClick" />
   <!--  
    <Button
        android:id="@+id/btContact"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="@string/Contact"
        android:onClick="onClick" />
     -->

</TableRow>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜