您的位置:首页 > 移动开发 > Android开发

android 在标题栏中添加控件

2015-01-19 19:50 10 查看
</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)

Added in
API level 11

Set selected display options. Only the options specified by mask will be changed. To change all display option bits at once, see
setDisplayOptions(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 disable
DISPLAY_USE_LOGO
.

Parameters
optionsA combination of the bits defined by the DISPLAY_ constants defined in ActionBar.
maskA bit mask declaring which display options should be changed.

public abstract void setDisplayOptions(int options)

Added in
API level 11

Set display options. This changes all display option bits at once. To change a limited subset of display options, see
setDisplayOptions(int,
int)
.

Parameters
optionsA 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("");

这样就不会报空指针异常了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: