简体   繁体   English

如何从另一个视图获取值并在同一活动中使用它?

[英]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代码

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.

相关问题 如何从另一个班级的活动/班级获取视图? - How to get the view from an activity/class from another class? 如何在同一活动中从AsyncTask到另一个方法获取整数数组? - How to get the integer arrays from AsyncTask to another method in same activity? 如何在 recyclerview 中提供 onclick 以从 mysql 获取另一个活动的值 - how to give onclick in recyclerview to get values to another activity from mysql 如何从第一个活动的文本视图中获取数据并从另一个活动中发布数据? - How to get data from text view in first activity and post it from another activity? 如何使用一个活动中的按钮添加到另一个活动中的列表视图? - How could I use the button from in one activity to add to list view in another activity? 从一个屏幕(活动)导航到另一个活动时,我们如何获取和设置相同的 Toggle 值? - How do we get and set same Toggle value while navigating from one screen(activity) to another activity? 如何获取 Recycler View 中的 TextViews 并将其发送到另一个活动并将其添加到 arraylist 值 - How to get the TextViews inside the Recycler View and send it to another activity and add it to the arraylist values 如何从另一个活动获取 ArrayList 到另一个 - How to get ArrayList from another activity to Another 如何获得年龄并在另一项活动中使用它? - how to get age and use it in another activity? 单击另一个活动的按钮后修改文本视图值 - Modify Text View values after clicking a button from another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM