如何在编辑文本字段后按下按钮?

时间:2013-07-19 10:50:50

标签: android button

我有一段代码显示静态文本,编辑文本作为搜索框,提交按钮提交查询。

代码类似:

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 */
            }
        });
    }
}

活动屏幕出现如下:

enter image description 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>

如您所见,提交按钮与编辑文本搜索框重叠。如何让按钮元素降下来?

3 个答案:

答案 0 :(得分:0)

在你的

中使用android:layout_alignParentBottom =“true”

答案 1 :(得分:0)

添加 android:以下参考

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

答案 2 :(得分:0)

最好在xml布局中使用LinearLayout 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 Documentation