无法在edittext中控制光标位置

时间:2014-01-24 17:22:36

标签: android android-edittext

我正在练习Android开发,我正在尝试关注如何制作Twitter客户端应用程序的指南。我实现了我的撰写活动,但是当我运行应用程序时,光标显示在远离第一行的位置。我已经遵循了很多关于stackoverflow的技巧来修复它但不能

这是一个截图:http://t.co/qNA0gssOUG

ComposeActivity.java

package codepath.exercises.tweets;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class ComposeActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compose);
    final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
    Button tweetButton = (Button) findViewById(R.id.tweet_button);
    Button cancelButton = (Button) findViewById(R.id.cancel_button);
    final EditText tweetEditText = (EditText) findViewById(R.id.edittext_tweet);
    tweetEditText.requestFocus();
    tweetEditText.setSelection(0);
    tweetButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            imm.hideSoftInputFromWindow(tweetEditText.getWindowToken(), 0);
        }

    });
    cancelButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            imm.hideSoftInputFromWindow(tweetEditText.getWindowToken(), 0);
        }

    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.compose, menu);
    return true;
}

}

activity_compose.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:background="#efefef"
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=".ComposeActivity" >

<Button
    android:id="@+id/cancel_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/edittext_tweet"
    android:layout_alignParentTop="true"
    android:layout_centerVertical="true"
    android:text="@string/cancel_button" />

<EditText
    android:id="@id/edittext_tweet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/cancel_button"
    android:layout_marginTop="10dp"
    android:background="#ffffff"
    android:inputType="textMultiLine" />

<Button
    android:id="@+id/tweet_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/cancel_button"
    android:layout_alignBottom="@+id/cancel_button"
    android:layout_alignRight="@id/edittext_tweet"
    android:text="@string/tweet_button" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

如果我理解正确,你所看到的是EditText的正常行为。光标出现在中心线上。

答案 1 :(得分:0)

为edittext设置重力顶部

<EditText
    android:id="@id/edittext_tweet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/cancel_button"
    android:layout_marginTop="10dp"
    android:background="#ffffff"
    android:gravity="top"
    android:inputType="textMultiLine" />

答案 2 :(得分:0)

您可以添加

android:gravity="top" 

到Xml中的EditText

相关问题