【问题标题】:Edit text not receiving text编辑文本不接收文本
【发布时间】:2023-03-17 08:51:01
【问题描述】:

我已经尝试解决它几个小时,但无法解决。发生的事情是我设置了这个editText,称为活动上的布局,并将其连接到活动上的我的变量,当您单击该字段时,它会打开可视键盘,但是当您按下一个键时,它会带您到类似浏览器的搜索屏幕,而不仅仅是更新edittext的文本。

在我的 xml 中,editText 是:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background = "@drawable/splash"
  android:orientation = "vertical">

  <EditText 
    android:id="@+id/searchEdit"
    android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="@color/black"
android:textColorHint="@color/hintGray"
android:textSize="20dp"
android:paddingLeft="50dp"
android:paddingRight="10dp"
android:layout_marginTop="300dp"
android:background="@drawable/a01_text_box"
android:hint="Hint"
android:layout_gravity="center_horizontal"   />

  <ImageButton 
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/a01_boton_buscar"
android:layout_gravity="center_horizontal"  /> 

</LinearLayout>

关于我这样做的活动

public class SubscribeActivity {

private EditText searchEdit;
private ImageButton searchButton;
private Context context;

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.subscribe_main);

        this.context = this;

        findAndInitViews();

}

protected void findAndInitViews() {
    searchEdit = (EditText) findViewById(R.id.searchEdit);
    searchButton = (ImageButton) findViewById(R.id.searchButton);

    searchButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            String codeText = searchEdit.getText().toString();
            Toast.makeText(context, codeText, Toast.LENGTH_LONG).show();
        }
    });

}

}

我仍然找不到不让它发生的方法。你能帮帮我吗?

【问题讨论】:

  • 你在尝试findViewById(id)之前有没有打电话给setContentView(R.layout.yourLayoutFromEditText)
  • 是的,在那里更新了所有代码
  • @ArielCapozzoli 看来你没有扩展Activity类,你正在尝试使用Activity的onCreate()方法。
  • 实际上它确实从扩展 Activity 类的主要活动扩展而来,但我不确定我是否可以发布该信息,所以我在发布前删除了扩展

标签: android android-edittext


【解决方案1】:

看起来你实际上并没有在玩活动

这是错误的:

public class SubscribeActivity

应该是这样的:

public class SubscribeActivity extends Activity

【讨论】:

  • 实际上它确实从扩展 Activity 类的主要活动扩展而来,但我不确定我是否可以发布该信息,所以我在发布前删除了扩展
  • 然后清理你的项目并再试一次
【解决方案2】:

首先我建议使用getApplicationContext() Activity 方法 - 而不是将 Activity 转换为您的 Context-Object。除此之外,您必须扩展Activity。我想知道您的代码正在运行...但我认为这是复制和粘贴错误。

您的onCreate(Bundle b) 应该是公开的而不是受保护的。

如果故障仍然存在,我将注释显示 EditText 不需要的代码。然后一步一步将代码重新包含回来。

【讨论】:

  • 好的,使用评论建议,发现当从启动活动打开一个progressDialog时会发生这种情况,这很奇怪,因为一旦完成这个意图,它就会自行完成......我删除了该活动的对话框,但没有找到发生此问题的原因。这解决了我的问题
【解决方案3】:

尝试不同的方式。

     public class SubscribeActivity extends Activity{

    private EditText searchEdit;
    private ImageButton searchButton;


    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.subscribe_main);

    searchEdit = (EditText) findViewById(R.id.searchEdit);
    searchButton = (ImageButton) findViewById(R.id.searchButton);    
 }

searchButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        String codeText = searchEdit.getText().toString();
        Toast.makeText(SubscribeActivity.this, codeText, Toast.LENGTH_LONG).show();
    }
});

  }
 }

请尝试这样做并告诉我结果。

【讨论】:

  • @ArielCapozzoli 我已经更正了我的代码,请看一下。你在 logcat 中遇到了什么错误。
  • 就是这样,logcat没有报错,它只是尝试同时打开全局搜索、联系人搜索和位置搜索...
  • @ArielCapozzoli 尝试使用 Project > Clean 来清理您的项目。要获取 logcat,请按照步骤转到 1. Windows >Open Perspective > DDMS 2. 转到设备选项卡并单击您当前运行设备。您将获得 logcat。希望对您有所帮助.......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多