为什么我的 Android Studio EditText 视图不显示输入、输出或提示?

huangapple go评论67阅读模式
英文:

Why does my android studio EditText view not display input, output, or the hint?

问题

//DOES NOT SHOW INPUT AT TIME OF INPUT, OR WHEN TOLD TOLD BY OUTPUT. HINT DOES NOT SHOW EITHER-------
<EditText
android:id="@+id/MetalCounter"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_margin="4dp"
android:ems="10"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="15sp"
android:hint="0"
android:textColorHint="#000000"
android:inputType="number"
app:layout_constraintRight_toLeftOf="@id/guideline"
app:layout_constraintTop_toBottomOf="@id/ResourceSubmitButton" />

//is put in top left corner for testing purposes---------------------------------
<TextView
android:id="@+id/MetalResult"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_margin="4dp"
android:text=""
android:textAlignment="center"
android:textColor="#000000"
android:textSize="15sp"
/>
package com.example.game;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

// These are the global variables
EditText MetalCounter;
TextView MetalResult;
Button ResourceSubmitButton;
int Metal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //to get from user input and into variable form
    MetalCounter = (EditText) findViewById(R.id.MetalCounter);
    MetalResult = (TextView) findViewById(R.id.MetalResult);
    ResourceSubmitButton = (Button) findViewById(R.id.ResourceSubmitButton);

    //Submit button
    ResourceSubmitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //to receive and show the inputted values into the MetalResult TextView
            MetalResult.setText(MetalCounter.getText().toString());

            //This is a test to see if the result would show up but does not
            Metal = Integer.parseInt(MetalCounter.getText().toString());
            MetalCounter.setText(MetalCounter.getText().toString());
        }
    });
}
}
英文:

My EditText does not work like the videos and articles I have found. The EditText does not show what the user inputted at the time of input or when I tell the output to show there, the hint I assigned it also does not show up. Right now I have the output going to a TextView but can greatly reduce lines of code if I can get the EditText working properly. I am new to this and have troubles understanding. Thank you for the help.

Here is the xml file:

//DOES NOT SHOW INPUT AT TIME OF INPUT, OR WHEN TOLD TOLD BY OUTPUT. HINT DOES NOT SHOW EITHER-------
&lt;EditText
android:id=&quot;@+id/MetalCounter&quot;
android:layout_width=&quot;40dp&quot;
android:layout_height=&quot;20dp&quot;
android:layout_margin=&quot;4dp&quot;
android:ems=&quot;10&quot;
android:textAlignment=&quot;center&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;15sp&quot;
android:hint=&quot;0&quot;
android:textColorHint=&quot;#000000&quot;
android:inputType=&quot;number&quot;
app:layout_constraintRight_toLeftOf=&quot;@id/guideline&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/ResourceSubmitButton&quot; /&gt;

 //is put in top left corner for testing purposes---------------------------------
&lt;TextView
android:id=&quot;@+id/MetalResult&quot;
android:layout_width=&quot;40dp&quot;
android:layout_height=&quot;20dp&quot;
android:layout_margin=&quot;4dp&quot;
android:text=&quot;&quot;
android:textAlignment=&quot;center&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;15sp&quot;
 /&gt;

and here is the java file:

package com.example.game;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

// These are the global variables
EditText MetalCounter;
TextView MetalResult;
Button ResourceSubmitButton;
int Metal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //to get from user input and into variable form
    MetalCounter = (EditText) findViewById(R.id.MetalCounter);
    MetalResult = (TextView) findViewById(R.id.MetalResult);
    ResourceSubmitButton = (Button) findViewById(R.id.ResourceSubmitButton);

    //Submit button
    ResourceSubmitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //to receive and show the inputted values into the MetalResult TextView
            MetalResult.setText(MetalCounter.getText().toString());
   

            //This is a test to see if the result would show up but does not
            Metal = Integer.parseInt(MetalCounter.getText().toString());
            MetalCounter.setText(MetalCounter.getText().toString());
        }
    });
}
}

答案1

得分: 0

我认为问题出在这部分代码中:

android:layout_width="40dp"
android:layout_height="20dp"
android:layout_margin="4dp"
android:ems="10"
android:textSize="15sp"

这些属性定义不清楚,因为你将 layout_margin 设置为 4dp,这导致视图收缩。所以现在宽度不再是 40dp,而是变成了 32dp,类似地,高度仅有 12dp,非常小,与选定的 textSize 不匹配。

因此,我建议将宽度和高度属性更改为以下的 wrap_content

android:layout_width="wrap_content"
android:layout_height="wrap_content"

其他部分可以保持不变。

备注

  • 考虑将变量改为遵循 camelCase 命名约定
// Before
EditText MetalCounter;
TextView MetalResult;

//After
EditText metalCounter;
TextView metalResult;
  • 改变提示文字颜色的透明度,以便与文本本身区分开来
  • 如果问题仍然存在,可能是底部的约束导致的。如果是这样,请发布完整的布局文件。
英文:

Well, I think the problem lies here in this part

android:layout_width=&quot;40dp&quot;
android:layout_height=&quot;20dp&quot;
android:layout_margin=&quot;4dp&quot;
android:ems=&quot;10&quot;
android:textSize=&quot;15sp&quot;

These attributes are not well-defined because you have set the layout_margin to 4dp which causes the view to shrink. so instead of having a width of 40dp, now you have a width of 32dps, similarly you have a height of only 12dps which is very small and doesn't match with the selected textSize

So, my suggestion is changing the width and height attributes to wrap_content as follows

android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;

and you can keep everything else as it's.

Remarks

  • Consider changing your variables to follow the camelCase convention
// Before
EditText MetalCounter;
TextView MetalResult;

//After
EditText metalCounter;
TextView metalResult;
  • Change the hint color's opacity in order to differentiate it from the text itself
  • If you still have a problem so it may be due to the constraints in the bottom. If so, please post the full layout file.

huangapple
  • 本文由 发表于 2020年10月3日 22:25:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64185266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定