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

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

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

问题

  1. //DOES NOT SHOW INPUT AT TIME OF INPUT, OR WHEN TOLD TOLD BY OUTPUT. HINT DOES NOT SHOW EITHER-------
  2. <EditText
  3. android:id="@+id/MetalCounter"
  4. android:layout_width="40dp"
  5. android:layout_height="20dp"
  6. android:layout_margin="4dp"
  7. android:ems="10"
  8. android:textAlignment="center"
  9. android:textColor="#000000"
  10. android:textSize="15sp"
  11. android:hint="0"
  12. android:textColorHint="#000000"
  13. android:inputType="number"
  14. app:layout_constraintRight_toLeftOf="@id/guideline"
  15. app:layout_constraintTop_toBottomOf="@id/ResourceSubmitButton" />
  16. //is put in top left corner for testing purposes---------------------------------
  17. <TextView
  18. android:id="@+id/MetalResult"
  19. android:layout_width="40dp"
  20. android:layout_height="20dp"
  21. android:layout_margin="4dp"
  22. android:text=""
  23. android:textAlignment="center"
  24. android:textColor="#000000"
  25. android:textSize="15sp"
  26. />
  1. package com.example.game;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8. public class MainActivity extends AppCompatActivity {
  9. // These are the global variables
  10. EditText MetalCounter;
  11. TextView MetalResult;
  12. Button ResourceSubmitButton;
  13. int Metal;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. //to get from user input and into variable form
  19. MetalCounter = (EditText) findViewById(R.id.MetalCounter);
  20. MetalResult = (TextView) findViewById(R.id.MetalResult);
  21. ResourceSubmitButton = (Button) findViewById(R.id.ResourceSubmitButton);
  22. //Submit button
  23. ResourceSubmitButton.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View view) {
  26. //to receive and show the inputted values into the MetalResult TextView
  27. MetalResult.setText(MetalCounter.getText().toString());
  28. //This is a test to see if the result would show up but does not
  29. Metal = Integer.parseInt(MetalCounter.getText().toString());
  30. MetalCounter.setText(MetalCounter.getText().toString());
  31. }
  32. });
  33. }
  34. }
英文:

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:

  1. //DOES NOT SHOW INPUT AT TIME OF INPUT, OR WHEN TOLD TOLD BY OUTPUT. HINT DOES NOT SHOW EITHER-------
  2. &lt;EditText
  3. android:id=&quot;@+id/MetalCounter&quot;
  4. android:layout_width=&quot;40dp&quot;
  5. android:layout_height=&quot;20dp&quot;
  6. android:layout_margin=&quot;4dp&quot;
  7. android:ems=&quot;10&quot;
  8. android:textAlignment=&quot;center&quot;
  9. android:textColor=&quot;#000000&quot;
  10. android:textSize=&quot;15sp&quot;
  11. android:hint=&quot;0&quot;
  12. android:textColorHint=&quot;#000000&quot;
  13. android:inputType=&quot;number&quot;
  14. app:layout_constraintRight_toLeftOf=&quot;@id/guideline&quot;
  15. app:layout_constraintTop_toBottomOf=&quot;@id/ResourceSubmitButton&quot; /&gt;
  16. //is put in top left corner for testing purposes---------------------------------
  17. &lt;TextView
  18. android:id=&quot;@+id/MetalResult&quot;
  19. android:layout_width=&quot;40dp&quot;
  20. android:layout_height=&quot;20dp&quot;
  21. android:layout_margin=&quot;4dp&quot;
  22. android:text=&quot;&quot;
  23. android:textAlignment=&quot;center&quot;
  24. android:textColor=&quot;#000000&quot;
  25. android:textSize=&quot;15sp&quot;
  26. /&gt;

and here is the java file:

  1. package com.example.game;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8. public class MainActivity extends AppCompatActivity {
  9. // These are the global variables
  10. EditText MetalCounter;
  11. TextView MetalResult;
  12. Button ResourceSubmitButton;
  13. int Metal;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. //to get from user input and into variable form
  19. MetalCounter = (EditText) findViewById(R.id.MetalCounter);
  20. MetalResult = (TextView) findViewById(R.id.MetalResult);
  21. ResourceSubmitButton = (Button) findViewById(R.id.ResourceSubmitButton);
  22. //Submit button
  23. ResourceSubmitButton.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View view) {
  26. //to receive and show the inputted values into the MetalResult TextView
  27. MetalResult.setText(MetalCounter.getText().toString());
  28. //This is a test to see if the result would show up but does not
  29. Metal = Integer.parseInt(MetalCounter.getText().toString());
  30. MetalCounter.setText(MetalCounter.getText().toString());
  31. }
  32. });
  33. }
  34. }

答案1

得分: 0

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

  1. android:layout_width="40dp"
  2. android:layout_height="20dp"
  3. android:layout_margin="4dp"
  4. android:ems="10"
  5. android:textSize="15sp"

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

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

  1. android:layout_width="wrap_content"
  2. android:layout_height="wrap_content"

其他部分可以保持不变。

备注

  • 考虑将变量改为遵循 camelCase 命名约定
  1. // Before
  2. EditText MetalCounter;
  3. TextView MetalResult;
  4. //After
  5. EditText metalCounter;
  6. TextView metalResult;
  • 改变提示文字颜色的透明度,以便与文本本身区分开来
  • 如果问题仍然存在,可能是底部的约束导致的。如果是这样,请发布完整的布局文件。
英文:

Well, I think the problem lies here in this part

  1. android:layout_width=&quot;40dp&quot;
  2. android:layout_height=&quot;20dp&quot;
  3. android:layout_margin=&quot;4dp&quot;
  4. android:ems=&quot;10&quot;
  5. 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

  1. android:layout_width=&quot;wrap_content&quot;
  2. 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
  1. // Before
  2. EditText MetalCounter;
  3. TextView MetalResult;
  4. //After
  5. EditText metalCounter;
  6. 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:

确定