在视图下方显示烤面包部件

编程入门 行业动态 更新时间:2024-10-26 02:25:51
本文介绍了在视图下方显示烤面包部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 对于那些曾经帮助过我这个项目的人,非常感谢你!我的代码不再有任何问题,我做了额外的调整。现在应用程序现在实际上很强大,我想再做一件事。

查看布局在这里。

通常,烤面包视图出现在屏幕的底部中心。 OnClick被调用后,我想让它出现在下面(8dp)Submit按钮。如何实现这一点。

请参阅我的更新完成项目在这里。

package com.lookjohn.guessnumber; import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity {随机随机; 按钮按钮; EditText文字; int input; int MIN,MAX; int comp; int guesses; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); random = new Random(); button =(Button)findViewById(R.id.button1); text =(EditText)findViewById(R.id.editText1); MIN = 1; MAX = 100; comp = random.nextInt(MAX - MIN + 1)+ MIN; //生成1到100之间的随机数。 guesses = 0; button.setOnClickListener(myhandler1); } View.OnClickListener myhandler1 = new View.OnClickListener(){ public void onClick(View v){ / /实现最大数量的猜测,检测 //数量的猜测并从方法返回。 String value = text.getText()。toString(); //从editTextView的输入获取值 //如果用户提交一个空的EditText,返回以防止崩溃。 if(value.isEmpty()){ Toast.makeText(MainActivity.this,你必须输入一个猜测!,Toast.LENGTH_SHORT); return; } input = Integer.parseInt(value); //将字符串转换为整数 guesses ++; if(input> 100){ Toast.makeText(MainActivity.this,该数字大于100. Not valid!, Toast.LENGTH_SHORT)。显示(); return; } else if(input< comp) Toast.makeText(MainActivity.this,你的数字太小了, Toast.LENGTH_SHORT).show(); else if(input> comp) Toast.makeText(MainActivity.this,你的数字太大了, Toast.LENGTH_SHORT).show(); else Toast.makeText(MainActivity.this,好工作!答案是+ comp +.\\\+ 你做了+猜猜+ guesses.\\\+ 重新启动应用再试一次。, Toast.LENGTH_LONG).show(); } }; @Override public boolean onCreateOptionsMenu(菜单菜单){ //扩充菜单;如果存在,则会将项目添加到操作栏。 getMenuInflater()。inflate(R.menu.main,menu); 返回true; } }

解决方案

从我学到的知识来看,这是我想到的最好的方案。如果(输入> 100){ Toast($)

toast = Toast.makeText(MainActivity.this, R.string.over_guess, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER,0,0); toast.show(); return; } else if(input< comp){ Toast toast = Toast.makeText(MainActivity.this, getString(R.string.guess_low,input), Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER,0,0); toast.show(); } else if(input> comp){ Toast toast = Toast.makeText(MainActivity.this, getString(R.string.guess_high,input), Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER,0,0); toast.show(); } else { Toast toast = Toast.makeText(MainActivity.this, getString(R.string.correct,comp,guesses), Toast.LENGTH_LONG ); toast.setGravity(Gravity.CENTER,0,0); toast.show(); }

For those who helped me out earlier regarding this project, thank you very much! My code no longer has any problems, and I made extra tweaks. Now that the app is actually robust now, I want to do one more thing.

See screenshot of the layout here.

Normally, the toast view appears at the bottom center of the screen. I want to make it appear just below (8dp) the Submit button once OnClick is called. How can I accomplish this.

See my updated complete project here.

package com.lookjohn.guessnumber; import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { Random random; Button button; EditText text; int input; int MIN, MAX; int comp; int guesses; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); random = new Random(); button = (Button)findViewById(R.id.button1); text = (EditText)findViewById(R.id.editText1); MIN = 1; MAX = 100; comp = random.nextInt(MAX - MIN + 1) + MIN; // Generate random number between 1 and 100. guesses = 0; button.setOnClickListener(myhandler1); } View.OnClickListener myhandler1 = new View.OnClickListener() { public void onClick(View v) { // Implemented max number of guesses, detect // number of guesses and return from the method. String value = text.getText().toString(); // Get value from input from editTextView // If user submits an empty EditText, return to prevent a crash. if (value.isEmpty()) { Toast.makeText(MainActivity.this, "You must enter a guess!", Toast.LENGTH_SHORT); return; } input = Integer.parseInt(value); // Turn string into integer guesses++; if (input > 100) { Toast.makeText(MainActivity.this, "That number is greater than 100. Not Valid!", Toast.LENGTH_SHORT).show(); return; } else if (input < comp) Toast.makeText(MainActivity.this, "Your number is too small.", Toast.LENGTH_SHORT).show(); else if (input > comp) Toast.makeText(MainActivity.this, "Your number is too big.", Toast.LENGTH_SHORT).show(); else Toast.makeText(MainActivity.this, "Good job! The answer was " + comp + ".\n" + "You made " + guesses + " guesses.\n" + "Restart the app to try again.", Toast.LENGTH_LONG).show(); } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }

解决方案

From what I learned, this is the best I came up with for the program. Doing this puts the Toast message in the center of the phone.

if(input > 100) { Toast toast = Toast.makeText(MainActivity.this, R.string.over_guess, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); return; } else if(input < comp) { Toast toast = Toast.makeText(MainActivity.this, getString(R.string.guess_low, input), Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } else if(input > comp) { Toast toast = Toast.makeText(MainActivity.this, getString(R.string.guess_high, input), Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } else { Toast toast = Toast.makeText(MainActivity.this, getString(R.string.correct, comp, guesses), Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); }

更多推荐

在视图下方显示烤面包部件

本文发布于:2023-10-16 11:45:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   部件   烤面包

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!