Analysis of Android ActionBar Clock Making Example
- 2021-07-13 06:15:08
- OfStack
This article example for everyone to share Android ActionBar to make the specific code of the clock, for your reference, the specific content is 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*****************/
// Pass id Find the corresponding MenuItem ( SearchView )
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView view = (SearchView) searchItem.getActionView();
// To SearchView Set up listening for text changes
view.setOnQueryTextListener(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 *****************/
// Pass id Find custom layout MenuItem
MenuItem customItem = menu.findItem(R.id.action_custom_layout);
// Pass menuItem Adj. getActionView () method to get 1 A View Object
View v = customItem.getActionView();
// It can be passed through v Adj. findViewById() Method to find the corresponding control in the custom layout
AnalogClock acClock = (AnalogClock) v.findViewById(R.id.acClock);
// To customItem Menu entry setting Expand Collapse Listening
customItem.setOnActionExpandListener(new OnActionExpandListener() {
/**
* Menu entry expanded
* Returns as true The menu representing the entry can be expanded
* @Override
*/
public boolean onMenuItemActionExpand(MenuItem item) {
txtResult.setText(" The clock is unfolded ");
return true;
}
/**
* Menu entry collapsed
* Returns as true The menu representing the entry can be collapsed
* @Override
*/
public boolean onMenuItemActionCollapse(MenuItem item) {
txtResult.setText(" The 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="100"
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="100"
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, I also shared the tutorial on the use of Android ActionBar, which can be used for reference.
The above is the whole content of this article, hoping to help everyone learn Android software programming.