[英]How to get values from another view and use it in same activity?
Hi I want to write a simple application for calculating the area of a triangle and I want the user to set the height and base of the triangle in Edittext
view and by clicking on a button
it shows the result in the Edittext
view down below but my application crashes. 嗨,我想编写一个简单的应用程序来计算三角形的面积,我希望用户在
Edittext
视图中设置三角形的高度和底边,然后单击button
它在下面的Edittext
视图中显示结果,但是我应用程序崩溃。 this is my code : java code 这是我的代码: java代码
public class Activity2 extends AppCompatActivity {
TextView txtmasahat;
EditText edttext_ghaede;
EditText edttext_ertefa;
Button btnmosalas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
txtmasahat=(TextView) findViewById(R.id.txtmasahat) ;
btnmosalas=(Button)findViewById(R.id.btnmosalas);
edttext_ertefa=(EditText)findViewById(R.id.edttext_ertefa);
edttext_ghaede=(EditText)findViewById(R.id.edttext_ghaede);
btnmosalas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int input1=edttext_ghaede.getInputType();
int input2=edttext_ertefa.getInputType();
double masahat=input1*input2/2;
}
});
I think this code will helps you. 我认为这段代码将对您有所帮助。
btnmosalas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input1=edttext_ghaede.getText().toString();
String input2=edttext_ertefa.getText().toString();
if(!TextUtils.isEmpty(input1)&&!TextUtils.isEmpty(input2))
{
Integer
Integer masahat=Integer.parseInt(input1)*Integer.parseInt(input2)/2;
}
});
.getInputType() does not return the int value you expect. .getInputType()不返回您期望的int值。 You can use the following code instead
您可以改用以下代码
public class Activity2 extends AppCompatActivity {
TextView txtmasahat;
EditText edttext_ghaede;
EditText edttext_ertefa;
Button btnmosalas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
txtmasahat=(TextView) findViewById(R.id.txtmasahat) ;
btnmosalas=(Button)findViewById(R.id.btnmosalas);
edttext_ertefa=(EditText)findViewById(R.id.edttext_ertefa);
edttext_ghaede=(EditText)findViewById(R.id.edttext_ghaede);
btnmosalas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input1Str=edttext_ghaede.getText().toString();
String input2Str=edttext_ertefa.getText().toString();
int input1 = Integer.parseInt(input1Str);
int input2 = Integer.parseInt(input2Str);
double masahat=input1*input2/2;
}
});
int input1=edttext_ghaede.getInputType();
What you need isn't getInputType()
. 您不需要的是
getInputType()
。 You need to use getText().toString()
to retrieve data from that `EditText'. 您需要使用
getText().toString()
从该`EditText'检索数据。
Also, you'll need to convert the String
you get into an Integer
. 另外,您需要将获得的
String
转换为Integer
。 For that, the complete statement will become: 为此,完整的语句将变为:
Integer.parseInt(edttext_ghaede.getText().toString());
Good luck. 祝好运。
edttext_ertefa=(EditText)findViewById(R.id.edttext_ertefa);
edttext_ghaede=(EditText)findViewById(R.id.edttext_ghaede);
btnmosalas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int input1=Integer.parseInt(edttext_ghaede.getText().toString().trim());
int input2=Integer.parseInt(edttext_ertefa..getText().toString().trim());
double masahat= input1 * input2/2;
}
});
Also set this to your xml inside .... 还将其设置为您的xml里面的XML。
android:inputType="number"
int input1=Integer.parseInt(edttext_ghaede.getText().toString());
Get a value from edittext using 使用以下方法从edittext获取值
int input1=Integer.parseInt(eedttext_ertefa.getText().toString());
instead of 代替
int input1=edttext_ghaede.getInputType();
InputType is use for what we ristrict to user to enter text/number like below code set inputtype number.
InputType用于限制用户输入文本/数字,如下面的代码集输入类型编号。
editText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.