android 在标题栏中添加控件


</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, 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
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, seesetDisplayOptions(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("");

这样就不会报空指针异常了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值