2013-07-19 36 views
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 */ 
      } 
     }); 
    } 
} 

活动屏幕出现这样的:

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> 

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

+1

向我们展示您的xml布局文件 –

+0

我已经提供了xml布局。 –

+2

android:layout_below =“@ + id/searchText” – NaserShaikh

回答

0

这将是更好的在你的XML布局使用LinearLayoutandroid: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

0

使用Android:layout_alignParentBottom = “真” 在你

0

添加机器人:下面参考

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