Android:将“编辑文本”字段链接到按钮

时间:2014-04-01 22:45:07

标签: java android android-edittext switch-statement

我正在创建一个时间表应用程序,其中一个活动允许用户输入他们想要查看的表的哪些时间,然后应用程序将调出那些时间表。(例如6x5 = 30)等。< / p>

下面是我为活动创建的xml布局:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:padding="15dp">

        <TextView
            android:id="@+id/tvTop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="I want to see the: "
            android:textSize="25dp" />

        <EditText
            android:id="@+id/etEnterNumber"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter Number..."
             >
        </EditText>

        <TextView
            android:id="@+id/tvBottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Times tables!"
            android:textSize="25dp" />

        <Button
            android:id="@+id/btnGo"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="Go" 
            android:layout_gravity="center"/>r

    </LinearLayout>

这就是我迄今为止为函数类创建的java类:

public class ViewTimesTables extends Activity implements View.OnClickListener {

        // Declaring Vars
        Button go;
        EditText enterNumber;
        TextView top;
        TextView bottom;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // setting equal to text layout View
            setContentView(R.layout.view);

            // calling method to intialise vars
            initialiseVars();

        }// on create end

        /**
         * method to initialise all of the buttons, textviews etc used to clean up
         * the onCreate.
         */
        private void initialiseVars() {
            // Setting up (initialising) all the buttons text views etc from the xml
            // (vid 25)
            go = (Button) findViewById(R.id.btnGo);
            enterNumber = (EditText) findViewById(R.id.etEnterNumber);
            top = (TextView) findViewById(R.id.tvTop);
            bottom = (TextView) findViewById(R.id.tvBottom);

        }

        /**
         * Method with on click listener that adds functionality for all of the
         * buttons, text views etc
         * 
         * @param v
         */
        public void onClick(View view) {

            // switch statement which determines what is clicked
            switch ((view).getId()) {
            case R.id.etEnterNumber:

                // code to read user number (i.e. between 1 and 12)
                //And possibly link to go button
                break;

            case R.id.btnGo:

                // code to bring up new activity/screen with times table
                // of the number that was entered in edit text

                break;

            }

        }

    }

我不确定如何添加正确的功能(可能在switch语句中),以便在例如在编辑文本框中输入“6”并按下“go”按钮,然后在新活动中显示6次表格?

3 个答案:

答案 0 :(得分:2)

我首先看Intents开始新活动并将数据传递给它。

相关教程是Android Intents Tutorial

从编辑文本中获取文本非常简单,只需enterNumber.getText().getString()

然后,您可以使用条件语句来调用指定的类。

这样的东西允许你将两个值传递给SixTimesTables类,传递值为5和6。

if(enterNumber.getText().getString().equals("6")){
  Intent i = new Intent(this, SixTimesTables.class);
  i.putExtra("Value1", 5);
  i.putExtra("Value2", 6);
  // set the request code to any code you like,
  // you can identify the callback via this code
  startActivityForResult(i, REQUEST_CODE);
}

答案 1 :(得分:0)

您可能想要下一个活动的动态布局。

它可能对你有所帮助。 http://www.dreamincode.net/forums/topic/130521-android-part-iii-dynamic-layouts/

然后你可以像AndyGable所提到的那样在活动之间切换。

希望它会帮助你。

答案 2 :(得分:0)

如果在editText中输入数据,你真的不需要你可以处理的editText的onClick,或者只是点击按钮点击这样:

public void onClick(View view) {

        // switch statement which determines what is clicked
        switch ((view).getId()) {
        case R.id.btnGo:

            // code to bring up new activity/screen with times table
            // of the number that was entered in edit text

            // check if editText has values or not
            if(TextUtils.isEmpty(mEditText.getText().toString())) {
                  mEditText.setError("Please enter a number");
             }else {
                  int number = Integer.parseInt(mEditText.getText().toString());
                  Intent intent = new Intent(YourCurrentActivity.this, NextActivity.class);
                  intent.putExtra("value", number);
                  startActivity(intent);

                  // it is always good to check if the value entered is a number only or not
                  // add inputType tag in the xml 
                  // android:inputType="number" for the editText.
             }

            break;

        }

    }

现在,为了在下一个活动中获得价值,请执行以下操作:

// write this inside the onCreate of the Activity.
int  number;
if(getIntent().getExtras() != null) {
      number = getIntent().getIntExtra("value");
}

// use the number then to display the tables