Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试使用OnClick方法将编辑文本和图像按钮添加到操作栏_Android_Button_Onclick_Android Actionbar - Fatal编程技术网

Android 尝试使用OnClick方法将编辑文本和图像按钮添加到操作栏

Android 尝试使用OnClick方法将编辑文本和图像按钮添加到操作栏,android,button,onclick,android-actionbar,Android,Button,Onclick,Android Actionbar,所以我尝试做一个类似Facebook应用程序和其他许多应用程序的动作栏。操作栏有一个功能,您可以在其中键入编辑文本,然后按保存按钮。然而,我的尝试到目前为止失败了。布局是使用app:actionLayout的menu.xml,它引用了项目的我的布局。但当我将应用程序放在OnCreate下时,它崩溃了,在onCreateOptions菜单下调用时,它也不起作用。有人能帮忙吗 布局如下: <RelativeLayout xmlns:android="http://schemas.android

所以我尝试做一个类似Facebook应用程序和其他许多应用程序的动作栏。操作栏有一个功能,您可以在其中键入编辑文本,然后按保存按钮。然而,我的尝试到目前为止失败了。布局是使用app:actionLayout的menu.xml,它引用了项目的我的布局。但当我将应用程序放在OnCreate下时,它崩溃了,在onCreateOptions菜单下调用时,它也不起作用。有人能帮忙吗

布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:weightSum="1">

  <ImageButton
    android:layout_width="57dp"
    android:layout_height="48dp"
    android:id="@+id/postButton"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_action_send_now"
    />

  <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editMessage"
    android:layout_alignBottom="@+id/postButton"
    android:layout_toLeftOf="@+id/postButton"
    android:layout_toStartOf="@+id/postButton"/>


</RelativeLayout>

我知道它是怎么掉下来的。我试图把它放在动作栏中,但你可以通过添加一个片段,然后将其强制放到屏幕底部来完成

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  tools:context=".MainActivity">

<item android:id="@+id/action_search"
      android:title="@string/search_title"
      android:icon="@drawable/ic_action_search"
      app:showAsAction="ifRoom|collapseActionView"
      app:actionViewClass="android.support.v7.widget.SearchView"/>

<item android:id="@+id/action_message"
      app:actionLayout="@layout/message_fragment"
      app:showAsAction="always|collapseActionView"
      android:icon="@drawable/ic_action_chat"
      android:title="@string/message_item"/>

<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:icon="@drawable/ic_action_overflow"
      android:orderInCategory="100"
      app:showAsAction="always"/>
</menu>
@Override
 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater ().inflate (R.menu.menu_main, menu);

    View view = menu.findItem(R.id.action_message).getActionView();

    view.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick(View v) {

        }
    });

    final EditText editMessage = (EditText) findViewById (R.id.editMessage);


    final ImageButton actionSendButton = (ImageButton) findViewById (R.id.postButton);


    actionSendButton.setOnClickListener (new View.OnClickListener () {
        @Override
        public void onClick(View v) {
            Post post = getCurrentPost ();

            post.setPost (editMessage.getText ().toString ());

            post.setGamer (ParseUser.getCurrentUser ());

            post.saveInBackground (new SaveCallback () {
                @Override
                public void done(ParseException e) {
                    if (e == null) {
                        setResult (Activity.RESULT_OK);
                        finish ();
                    } else {

                        Context context = getApplicationContext();
                        CharSequence text = "Posting and Thank You!";
                        int duration = Toast.LENGTH_SHORT;

                        Toast.makeText (context, text, duration).show ();
                    }

                }
            });

        }
    });

    return true;
}