如何获取另一种方法中定义的编辑文本的值?

时间:2016-07-13 08:11:19

标签: java android

我的应用程序(API 11)根据用户输入创建EditTexts并为每个提供一个Id。当用户填写每个编辑文本时,我需要执行空检查,获取并将数据发送到下一个活动。但是现在我被卡住了,因为我无法提及它们。我该怎么办?

由于

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.wima.civilengineeringcalculator.twodtruss"
tools:showIn="@layout/activity_twodtruss"
android:orientation="vertical"
android:background="#4A148C"
android:layoutDirection="ltr">

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:layout_alignParentTop="true"
    android:id="@+id/scr1">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="تحلیل خرپاهای دو بعدی:"
            android:textColor="#FFFFFF"
            android:id="@+id/textView1"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"/>
        <View
            android:layout_height="2dip"
            android:layout_width="wrap_content"
            android:layout_below="@+id/textView1"
            android:background="#4A148C"
            android:id="@+id/V3"/>
        <View
            android:layout_height="2dip"
            android:layout_width="wrap_content"
            android:layout_below="@+id/V3"
            android:background="#4A148C"
            android:id="@+id/V5"/>
        <View
            android:layout_height="2dip"
            android:layout_width="wrap_content"
            android:layout_below="@+id/V5"
            android:background="#FF909090"
            android:id="@+id/V4"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="تعداد اعضا:"
            android:textColor="#FFFFFF"
            android:id="@+id/textView4"
            android:layout_below="@+id/V4"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/textView4"
            android:layout_toStartOf="@+id/textView4"
            android:textColor="#FFFFFF"
            android:ems="10"
            android:id="@+id/editT"
            android:layout_below="@+id/V4"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:inputType="number" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="تعداد گره های سازه:"
            android:id="@+id/textView5"
            android:textColor="#FFFFFF"
            android:layout_below="@+id/editT"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:textColor="#FFFFFF"
            android:id="@+id/editT2"
            android:layout_below="@+id/editT"
            android:layout_toLeftOf="@+id/textView5"
            android:layout_toStartOf="@+id/textView5"
            android:layout_alignLeft="@+id/editT"
            android:layout_alignStart="@+id/editT"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="تعداد مقاطع مورد نیاز:"
            android:textColor="#FFFFFF"
            android:id="@+id/textView6"
            android:layout_below="@+id/editT2"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:id="@+id/editT3"
            android:textColor="#FFFFFF"
            android:layout_below="@+id/editT2"
            android:layout_alignLeft="@+id/editT"
            android:layout_toLeftOf="@+id/textView6"
            android:layout_alignStart="@+id/editT"
            android:layout_toStartOf="@+id/textView6"
            android:inputType="number" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="مرحله بعد"
            android:textColor="#000000"
            android:background="@drawable/btn_selector"
            android:id="@+id/button5"
            android:layout_below="@+id/editT3"
            android:layout_alignLeft="@+id/editT3"
            android:layout_alignRight="@+id/editT3"
            android:layout_alignStart="@+id/editT3"
            android:layout_alignEnd="@+id/editT3"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="راهنما"
            android:textColor="#000000"
            android:background="@drawable/btn_selector"
            android:id="@+id/button6"
            android:layout_below="@+id/editT3"
            android:layout_alignLeft="@+id/textView5"
            android:layout_alignRight="@+id/textView4"
            android:layout_alignStart="@+id/textView5"
            android:layout_alignEnd="@+id/textView4"/>
        <View
            android:layout_height="2dip"
            android:layout_width="wrap_content"
            android:layout_below="@+id/button5"
            android:background="#4A148C"
            android:id="@+id/V1"/>
        <View
            android:layout_height="2dip"
            android:layout_width="wrap_content"
            android:layout_below="@+id/V1"
            android:background="#FF909090"
            android:id="@+id/V2"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="مشخصات مقاطع را وارد نمایید:"
            android:textColor="#FFFFFF"
            android:id="@+id/textView7"
            android:layout_below="@+id/V1"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"/>

        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/sectable"
            android:layout_below="@+id/textView7">
        </TableLayout>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="مرحله بعد"
            android:textColor="#000000"
            android:background="@drawable/btn_selector"
            android:id="@+id/button1"
            android:layout_below="@+id/sectable"
            android:layout_alignLeft="@+id/button6"
            android:layout_alignRight="@+id/button6"
            android:layout_alignStart="@+id/button6"
            android:layout_alignEnd="@+id/button6"
            android:onClick="next1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="شروع مجدد"
            android:textColor="#000000"
            android:background="@drawable/btn_selector"
            android:id="@+id/button2"
            android:layout_below="@+id/sectable"
            android:layout_alignLeft="@+id/editT3"
            android:layout_alignRight="@+id/editT3"
            android:layout_alignStart="@+id/editT3"
            android:layout_alignEnd="@+id/editT3"/>

    </RelativeLayout>
</ScrollView>

Java代码:

public class MatrixAnalysis extends AppCompatActivity {
private TableLayout mLayout;
private EditText noofsecs;
private EditText Noele;
private EditText Nonode;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_twodtruss);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Noele = (EditText) findViewById(R.id.editT);
    Nonode = (EditText) findViewById(R.id.editT2);
    noofsecs = (EditText) findViewById(R.id.editT3);
    title = (TextView) findViewById(R.id.textView1);
    Button bt_calculate1 = (Button) findViewById(R.id.button5);
    Button bt_help = (Button) findViewById(R.id.button6);
    Button bt_N1 = (Button) findViewById(R.id.button1);
    Button bt_reset = (Button) findViewById(R.id.button2);

    TextView inter = (TextView) findViewById(R.id.textView7);
    bt_help.setVisibility(View.INVISIBLE);
    bt_N1.setVisibility(View.INVISIBLE);
    bt_reset.setVisibility(View.INVISIBLE);
    inter.setVisibility(View.INVISIBLE);

bt_calculate1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if ( Noele.getText().toString().trim().equals("")
                    || Nonode.getText().toString().trim().equals("")
                    ||  noofsecs.getText().toString().trim().equals("")) {
                Snackbar.make(findViewById(android.R.id.content), "لطفا مقادیر تمامی متغیرها را وارد کنید ", Snackbar.LENGTH_LONG)
                        .show();
            }
            else {
                double noofsecs1 = Double.valueOf(noofsecs.getText().toString());
                for (nsecctr = 1; nsecctr < noofsecs1 + 1; nsecctr++) {
                    mLayout = (TableLayout) findViewById(R.id.sectable);
                    prepuserinput();
                }
            }
        }
    });

Double Dnoofsecs = 5.0;
int aRows = Dnoofsecs.intValue();
double [] Asec = new double[aRows];
double [] Esec = new double[aRows];
double [] Gsec = new double[aRows];
double [] Iysec = new double[aRows];
double [] Izsec = new double[aRows];
double [] Jsec = new double[aRows];

public void prepuserinput() {

    Button bt_calculate1 = (Button) findViewById(R.id.button5);
    Button bt_help = (Button) findViewById(R.id.button6);
    Button bt_N1 = (Button) findViewById(R.id.button1);
    Button bt_reset = (Button) findViewById(R.id.button2);
    TextView inter = (TextView) findViewById(R.id.textView7);
    Noele = (EditText) findViewById(R.id.editT);
    Nonode = (EditText) findViewById(R.id.editT2);
    noofsecs = (EditText) findViewById(R.id.editT3);
    bt_calculate1.setVisibility(View.INVISIBLE);
    bt_help.setVisibility(View.VISIBLE);
    bt_N1.setVisibility(View.VISIBLE);
    bt_reset.setVisibility(View.VISIBLE);
    inter.setVisibility(View.VISIBLE);
    Noele.setEnabled(false);
    Nonode.setEnabled(false);
    noofsecs.setEnabled(false);

    Intent i = getIntent();
    String pro = i.getStringExtra("Problem");
    double pro1 = Double.parseDouble(pro.replace(",", "."));


    int width = getApplicationContext().getResources().getDisplayMetrics().widthPixels;
    int width1 = width * 13 / 100;
    int width2 = width * 12 / 100;



    TableRow tr = new TableRow(this);
    tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.MATCH_PAREN));
        TextView textview = new TextView(this);
        textview.setId(nsecctr + 0);
        textview.setText(":" + nsecctr + "مقطع");
        textview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.MATCH_PARENT));
        textview.setTextColor(Color.parseColor("#FFFFFF"));
        tr.addView(textview);

        EditText A = new EditText(this);
        A.setId(nsecctr + 0);
        A.setHint("A");
        A.setLayoutParams(new TableRow.LayoutParams(width2, 50));
        A.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        A.setTextColor(Color.parseColor("#FFFFFF"));
        A.setHintTextColor(Color.parseColor("#bdbdbd"));
        //width, height
        tr.addView(A);

        EditText E = new EditText(this);
        E.setId(nsecctr + 0);
        E.setHint("E");
        E.setLayoutParams(new TableRow.LayoutParams(width2, 50));
        E.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        E.setTextColor(Color.parseColor("#FFFFFF"));
        E.setHintTextColor(Color.parseColor("#bdbdbd"));
        tr.addView(E);

        EditText G = new EditText(this);
        G.setId(nsecctr + 0);
        G.setHint("G");
        G.setLayoutParams(new TableRow.LayoutParams(width2, 50));
        G.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        G.setTextColor(Color.parseColor("#FFFFFF"));
        G.setHintTextColor(Color.parseColor("#bdbdbd"));
        tr.addView(G);

        EditText Iy = new EditText(this);
        Iy.setId(nsecctr + 0);
        Iy.setHint("Iy");
        Iy.setLayoutParams(new TableRow.LayoutParams(width1, 50));
        Iy.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        Iy.setTextColor(Color.parseColor("#FFFFFF"));
        Iy.setHintTextColor(Color.parseColor("#bdbdbd"));
        tr.addView(Iy);

        EditText Iz = new EditText(this);
        Iz.setId(nsecctr + 0);
        Iz.setHint("Iz");
        Iz.setLayoutParams(new TableRow.LayoutParams(width1, 50));
        Iz.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        Iz.setTextColor(Color.parseColor("#FFFFFF"));
        Iz.setHintTextColor(Color.parseColor("#bdbdbd"));
        tr.addView(Iz);

        EditText J = new EditText(this);
        J.setId(nsecctr + 0);
        J.setHint("J");
        J.setLayoutParams(new TableRow.LayoutParams(width2, 50));
        J.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        J.setTextColor(Color.parseColor("#FFFFFF"));
        J.setHintTextColor(Color.parseColor("#bdbdbd"));
        tr.addView(J);

    mLayout.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
            TableLayout.LayoutParams.MATCH_PARENT));
}

public void next1 (View view) {
    double noofsecs1 = Double.valueOf(noofsecs.getText().toString());
    Jnew = (EditText) findViewById(R.id.editT3);
    for (nsecctr = 1; nsecctr < noofsecs1 + 1; nsecctr++) {
        mLayout = (TableLayout) findViewById(R.id.sectable);
            Asec[nsecctr] = A.getId();
            Esec[nsecctr] = E.getId();
            Gsec[nsecctr] = G.getId();
            Iysec[nsecctr] = Iy.getId();
            Izsec[nsecctr] = Iz.getId();
            Jsec[nsecctr] = J.getId();
    }
}

2 个答案:

答案 0 :(得分:1)

您应该为所有HashMap

商店创建EditText
HashMap <String, EditText > mapEditText = new HashMap<String, EditText >();

每当您以编程方式创建EditText时,请记得将其放入HashMap

EditText A = new EditText(this);
...
mapEditText.put("A",A);

EditText E = new EditText(this);
...
mapEditText.put("E",E);
....

next方法中,您可以通过

访问EditText
public void next1 (View view) {
    ...
    Asec[nsecctr] = mapEditText.get("A").getId();
    Esec[nsecctr] = mapEditText.get("E").getId();

}

答案 1 :(得分:0)

尝试更改next1,如下所示

public void next1 (View view) {
    double noofsecs1 = Double.valueOf(noofsecs.getText().toString());
    Jnew = (EditText) findViewById(R.id.editT3);
    for (nsecctr = 1; nsecctr < noofsecs1 + 1; nsecctr++) {
        mLayout = (TableLayout) findViewById(R.id.sectable);
            Asec[nsecctr] = view.A.getId();
            Esec[nsecctr] = view.E.getId();
            Gsec[nsecctr] = view.G.getId();
            Iysec[nsecctr] = view.Iy.getId();
            Izsec[nsecctr] = view.Iz.getId();
            Jsec[nsecctr] = view.J.getId();
    }
}