java - Android:将编辑文本字段链接到按钮

标签 java android android-edittext switch-statement

我正在创建一个时间表应用程序,其中一项 Activity 允许用户输入他们想要查看的时间表,然后应用程序将显示该时间表。(例如 6x5=30)等

下面是我为该 Activity 创建的 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 个乘法表将在新 Activity 中显示?

最佳答案

我首先查看 Intents启动一个新 Activity 并向其传递数据。

相关教程是这个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);
}

关于java - Android:将编辑文本字段链接到按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22798347/

相关文章:

java - 在 jxta 中查找加载的模块/服务

java - SWT Canvas PaintListener() 被错误调用

java - 在 Android 中跨不同模块访问代码?

java - Android - 获取imageview内的图像以完全填充

android - Ble 与 MVVM 和架构组件,正确的方法?

android - adjustResize 和软键盘打开时保留 EditText 比例

android - 软键盘阻止 EditText

java - 如何将第三方api中的JSON对象转换为本地POJO

java - 谷歌云消息开发者 Android 代码崩溃

android - 为 Android 中的每个字符拆分 EditText