Android Actionbar Production Clock instance resolution _android

Source: Internet
Author: User

This example for you to share the Android Actionbar production clock specific code for your reference, the specific contents are as follows

1. Mainactivity.java

Package com.example.days19actionbar07custom;
 
Import COM.EXAMPLE.DAYS19ACTIONBAR07CUSTOM.R;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.MenuItem.OnActionExpandListener;
Import Android.view.View;
Import Android.widget.AnalogClock;
Import Android.widget.SearchView;
Import Android.widget.SearchView.OnQueryTextListener;
 
Import Android.widget.TextView;
 
  public class Mainactivity extends activity {private TextView txtresult = null;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 
    Setcontentview (R.layout.activity_main);
  Txtresult = (TextView) Findviewbyid (R.id.txtresult);
 
     
    @Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); /**************searchview*****************///By ID to find the corresponding MenuItem (searchview) MenuItem Searchitem = menu.fi Nditem (r.id.actiOn_search);
 
    Searchview view = (Searchview) Searchitem.getactionview ();  Set text-changed listening View.setonquerytextlistener for Searchview (new Onquerytextlistener () {@Override public boolean
      Onquerytextsubmit (String query) {return false; 
        @Override public boolean onquerytextchange (String newtext) {Txtresult.settext ("You are searching" + NewText);
      return false;
     
    }
    }); /************** Custom Entry *****************///Find custom layout by id MenuItem MenuItem Customitem = Menu.finditem (r.id.actio
     
    N_custom_layout);
     
    A View object View V = Customitem.getactionview () is obtained by means of the Getactionview () method of MenuItem.
     
    The corresponding control in the custom layout can be found by the Findviewbyid () method of V AnalogClock acclock = (analogclock) V.findviewbyid (R.id.acclock);
       Set the expand collapse listening Customitem.setonactionexpandlistener for the Customitem menu entry (new Onactionexpandlistener () {/**
     * The menu entry is expanded * Returns TRUE to represent the item menu can be expanded * @Override * * public boolean Onmenuitemactionexpand (MenuItem item) {Txtresult.settext ("clock is expanded");
      return true; /** * Menu entry is folded * Returns true to represent the item menu can be folded * @Override * * Public boolean OnMenu
        Itemactioncollapse (MenuItem Item) {Txtresult.settext ("clock is folded");
      return true;
    }
    });
  return true;
 }
 
}

2. Main.xml

<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
 
  <item
    android:id= "@+id/" Action_search "
    android:orderincategory="
    android:actionviewclass= "Android.widget.SearchView"
    Android:showasaction= "Always"
    android:title= "@string/it_search"/> <item android:id=
    "@+id/ Action_custom_layout "
    android:actionlayout=" @layout/my_layout "
    android:orderincategory=
    " android:icon= "@drawable/ic_launcher"
    android:showasaction= "Always|collapseactionview"
    android:title= "Layout"/>
 
</menu>

3. Activity_main.xml

<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=". Mainactivity ">
 
  <textview
    android:id=" @+id/txtresult "android:layout_width=" Wrap_content "
    android:layout_height= "wrap_content"
    android:text= "@string/hello_world"/>
 
</ Relativelayout>


4. My_layout.xml

<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=". Mainactivity ">
 
  <analogclock
    android:id=" @+id/acclock "android:layout_width=" Wrap_content "
    android:layout_height= "wrap_content"
    android:text= "@string/hello_world"/>
 
</ Relativelayout>

After the article also for you to share the Android Actionbar use tutorials, you can do reference learning.

This is the entire content of this article, I hope to learn more about Android software programming help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.