简体   繁体   English

在弹出窗口中设置EditText等于上一个活动的字符串-错误:(92,30)错误:类型不兼容:不能将double转换为String

[英]Setting EditText in popup equal to string of previous activity - Error:(92, 30) error: incompatible types: double cannot be converted to String

So I am making this knittingcalculator app. 所以我正在制作这个knittingcalculator应用。 based on a couple of inputs, the app outputs the result in a popup. 基于几个输入,应用程序会在弹出窗口中输出结果。 This is the code 这是代码

public class Rectangular extends Activity {

EditText length;
EditText width;
EditText edge;
EditText roll;
TextView tt;
Button calculate;
double w=0;
double x=0;
double y=0;
double z=0;
double v=0;


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



    public void onClick(View arg0) {
        final Button btnOpenPopup = (Button)findViewById(R.id.button8);
        LayoutInflater layoutInflater
                = (LayoutInflater)getBaseContext()
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.pop_up, null);
        final PopupWindow popupWindow = new PopupWindow(
                popupView,
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT);
        popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);


        Button btnDismiss = (Button)popupView.findViewById(R.id.button9);
        btnDismiss.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popupWindow.dismiss();
            }});

        popupWindow.showAsDropDown(btnOpenPopup, 50, -30);

    };





private void initControls()
{
    length=(EditText)findViewById(R.id.editText);
    width=(EditText)findViewById(R.id.editText2);
    edge=(EditText)findViewById(R.id.editText3);
    roll=(EditText)findViewById(R.id.editText4);
    tt=(TextView)findViewById(R.id.editText6);
    calculate=(Button)findViewById(R.id.button8);
    calculate.setOnClickListener(new Button.OnClickListener()
    {public void onClick
                (View v) { calculate();}});
}

 EditText input;
 EditText output;
 Button one;


private void calculate()
{
    w=Double.parseDouble(length.getText().toString());
    x=Double.parseDouble(width.getText().toString());
    y=Double.parseDouble(edge.getText().toString());
    v=Double.parseDouble(roll.getText().toString());
    z=((x+y+y)*(y+y+w))/v;
    tt.setText(Double.toString(z));
}

xml(popup): xml(弹出):

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="1"
    android:background="@android:color/darker_gray">

 <View
     android:layout_width="0dp"
     android:layout_height="1dp"
     android:layout_weight="0.4" />

 <EditText
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:inputType="numberDecimal"
     android:ems="10"
     android:id="@+id/editText6" />

 <View
     android:layout_width="0dp"
     android:layout_height="1dp"
     android:layout_weight="0.15" />

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/notice"
     android:id="@+id/textView7"
     android:textSize="18sp" />

 <View
     android:layout_width="0dp"
     android:layout_height="1dp"
     android:layout_weight="0.05" />

 <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/dones"
     android:id="@+id/button9"
     android:layout_gravity="right" />

 </LinearLayout>

Now, the problem that I am having is I can't seem to set the edittext in the popup equal to the calculation and I hope someone can help me or pointme in the right direction 现在,我遇到的问题是我似乎无法将弹出窗口中的edittext设置为等于计算值,我希望有人可以帮助我或将我指向正确的方向

UPDATE: So I got the editText to display the letter Z but I would like it to display the result, but when I do I get the exception of doubles cannot be converted to string at first line 更新:所以我让editText显示字母Z,但我希望它显示结果,但是当我这样做时,双精度的例外不能在第一行转换为字符串

private String result = (z);
 private void calculate()
{
    w=Double.parseDouble(length.getText().toString());
    x=Double.parseDouble(width.getText().toString());
    y=Double.parseDouble(edge.getText().toString());
    v=Double.parseDouble(roll.getText().toString());
    z=((x+y+y)*(y+y+w))/v;
    result = Double.toString(z);
}

any idea what to do? 你知道该怎么办吗?

Since the EditText is in your popup layout, you would have to find it through your inflated popup view. 由于EditText在您的弹出式布局中,因此您必须通过膨胀的弹出式视图找到它。

EditText tt = (EditText) popupView.findViewById(R.id.editText6);

And populate your EditText at the same time that you inflate the popup: 并在填充弹出窗口的同时填充EditText

tt.setText(Double.toString(z));

Set The PopupWindow property to set and edit the EditText value. 设置PopupWindow属性以设置和编辑EditText值。

popupWindow.update();
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM