</pre>需求:在标题栏中添加一个EditText、ActionBar<p></p><p></p><p>实现方法:</p><p><pre name="code" class="html">actionbar=getActionBar();
actionbar.setCustomView(R.layout.edittext00);
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
DISPLAY_SHOW_CUSTOM
Show the custom view if one has been set.
DISPLAY_SHOW_HOME
Show 'home' elements in this action bar, leaving more space for other navigation elements. This includes logo and icon.
public abstract void setDisplayOptions(int options, int mask)
Set selected display options. Only the options specified by mask will be changed. To change all display option bits at once, seesetDisplayOptions(int)
.
Example: setDisplayOptions(0, DISPLAY_SHOW_HOME) will disable the DISPLAY_SHOW_HOME
option. setDisplayOptions(DISPLAY_SHOW_HOME, DISPLAY_SHOW_HOME | DISPLAY_USE_LOGO) will enable DISPLAY_SHOW_HOME
and disableDISPLAY_USE_LOGO
.
Parameters
options | A combination of the bits defined by the DISPLAY_ constants defined in ActionBar. |
---|---|
mask | A bit mask declaring which display options should be changed. |
public abstract void setDisplayOptions(int options)
Set display options. This changes all display option bits at once. To change a limited subset of display options, seesetDisplayOptions(int, int)
.
Parameters
options | A combination of the bits defined by the DISPLAY_ constants defined in ActionBar. |
---|
其中R.layout.edittext00,只包含一个EditText,我们把这个布局添加到ActionBar上
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/edittext00"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/noteAlert"
>
</EditText>
</LinearLayout>
怎么在声明这个EditText控件
在activity中的onCreate()
public void onCreate(){
......
EditText title=(EditText) findViewById(R.id.edittext00);
......
if(flag==true){
saveToSDutil tool=new saveToSDutil();
title.setText(thetil);
content.setText(tool.openFile(thetil).toString());
}else{
title.setText("");
content.setText("");
}
在上面的程序中,当程序运行到if(){}中时会报空指针异常。检查发现是title没有初始化,不知道为什么?明明前面已经完成了初始化操作了
但是我们把if(){}改成这样
if(flag==true){
saveToSDutil tool=new saveToSDutil();
title = (EditText) findViewById(R.id.edittext00);
title.setText(thetil);
content.setText(tool.openFile(thetil).toString());
}else{
title = (EditText) findViewById(R.id.edittext00);
title.setText("");
content.setText("");
这样就不会报空指针异常了