1

我有一段代码,它显示了一个静态文本、一个作为搜索框的编辑文本和一个用于提交查询的提交按钮。

代码看起来像这样:

public class MyActivity extends Activity {

    TextView textView;
    private EditText edittext;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);
        textView = (TextView) findViewById(R.id.textView1);
        textView.setText("Enter your search String :");
        addKeyListener();
        addListenerOnButton();
    }

    //Implement the method
    public void addKeyListener() {
        // get edit text component
        edittext = (EditText) findViewById(R.id.searchText);
        // add a key listener to keep track user input
        edittext.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            // if key down and "enter" is pressed
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                && (keyCode == KeyEvent.KEYCODE_ENTER)) {

                // display a floating message
                Toast.makeText(RecipeActivity.this,
                    edittext.getText(), Toast.LENGTH_LONG).show();
                return true;
            } else if ((event.getAction() == KeyEvent.ACTION_DOWN)
                && (keyCode == KeyEvent.KEYCODE_9)) {

                // display a floating message
                Toast.makeText(RecipeActivity.this,
                    "Aboriginal text!", Toast.LENGTH_LONG).show();
                return true;
            }
            return false;
        }
     });
    }

    //Implement the button
    public void addListenerOnButton() {
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {

              /* I will capture the search string here */
            }
        });
    }
}

活动屏幕如下所示:

在此处输入图像描述

编辑: 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=".RecipeActivity" >

      <TextView 
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content">
      </TextView>

      <EditText
        android:id="@+id/searchText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:label="@string/search_label" 
        android:hint="@string/search_hint" >
        <requestFocus />
      </EditText>

      <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

</RelativeLayout>

如您所见,提交按钮与编辑文本搜索框重叠。我怎样才能让按钮元素下来?

4

3 回答 3

0

在你的使用 android:layout_alignParentBottom="true"

于 2013-07-19T11:02:01.643 回答
0

添加android:下面的参考

    <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:below="@+id/searchText"
                android:text="Submit" />
于 2013-07-19T11:11:13.413 回答
0

LinearLayout在您的 xml 布局中使用with会更好,android:orientation="vertical"因为它比RelativeLayout渲染便宜得多。

解决方案LinearLayout

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

  <TextView 
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

  <EditText
    android:id="@+id/searchText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit" />

</LinearLayout>

如果您必须使用RelativeLayout

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

  <TextView 
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

  <EditText
    android:id="@+id/searchText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/textView1"/>

  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/searchText"
    android:text="Submit" />

</RelativeLayout>

有关更多信息,请参阅RelativeLayout 文档

于 2013-07-19T11:12:41.717 回答