如何使用Android按钮将整数作为用户输入?

时间:2013-09-02 04:25:43

标签: java android button input integer

我正在查看一些在线教程,以便使用[here] [1]等按钮从用户那里获取输入,但这不是我想要的,并想知道你是否有人遇到类似的问题

我想让用户在单击按钮时提示您输入integer input ONLY ,我不希望屏幕上显示TextField等。用户单击了按钮。用户输入必须保存在int bias

任何帮助都将不胜感激。

7 个答案:

答案 0 :(得分:1)

无论您正在谈论的textField是什么,您都可以设置输入类型,如:

textFieldName.setRawInputType(Configuration.KEYBOARD_12KEY);

您可以直接在XML中设置它:

android:inputType="number"

此外,即使您创建TextField,也可以使用以下方法将可见性设置为Invisible:

textFieldName.setVisibility(View.INVISIBLE);

然后,当用户单击该按钮时,您可以使用以下方法将其设置为Visible:

textFieldName.setVisibility(View.VISIBLE);

您可以更改按钮单击时的可见性,如问题中所述。

答案 1 :(得分:1)

要在键盘上显示数字,请将此行android:inputType="number"添加到EditText

 <EditText android:id="@+id/edittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
   />

如果要将edittext显示到对话框中,请使用customdialog Dialogs Example并在按钮单击侦听器中使用mDialog.show()

此处mDialogDialog

的对象

答案 2 :(得分:1)

您可以在布局中添加此文本字段并设置其可见性"gone"

boolean isClicked  =  false;
String userInput = null;

Button grayScaleFilterButton = (Button) findViewById(R.id.filter1_button);
    grayScaleFilterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        if(isClicked){
         isClicked = false;
              //Got the input and hide the textfield.
             userInput  = textField.getText().toString();
             textField.setVisibility(View.GONE);

        }else{
              //make the textfield visible and user can enter the input.
          isClicked = true;
              textField.setInputType(InputType.TYPE_CLASS_NUMBER);
          textField.setVisibility(View.VISIBLE);
        //do something

        }


        }
    });

答案 3 :(得分:1)

按钮单击它会显示一个alterDialogue供用户输入。输入一些值后,它会在相应的文本视图中显示。这是完整的代码:

package com.example.testandroidapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView input1;
    String value = "";
    int bias;

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

        Button grayScaleFilterButton = (Button) findViewById(R.id.button1);
        input1 = (TextView) findViewById(R.id.textView1);

        grayScaleFilterButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                AlertDialog.Builder alert = new AlertDialog.Builder(v
                        .getContext());

                alert.setTitle("Title");
                alert.setMessage("Message");

                // Set an EditText view to get user input
                final EditText input = new EditText(MainActivity.this);
                alert.setView(input);

                alert.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                value = input.getText().toString();

                                // Convert the inputted string into Integer
                                bias = Integer.parseInt(value);
                            }
                        });

                alert.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                // Canceled.
                            }
                        });

                alert.show();

            }
        });

        input1.setText(value);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

答案 4 :(得分:1)

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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:padding="5dp"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/edtInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:visibility="gone" />

        <Button 
            android:id="@+id/btnInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Input"/>
    </LinearLayout>

</LinearLayout>

活动代码

public class MyInputActivity extends Activity implements OnClickListener {

    private EditText edtInput;
    private Button btnInput;

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

        edtInput = (EditText) findViewById(R.id.edtInput);
        btnInput = (Button) findViewById(R.id.btnInput);

        btnInput.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        if(edtInput.getVisibility() == View.VISIBLE){
            edtInput.setVisibility(View.GONE);
            // write your more code here on gone
        }else{
            edtInput.setVisibility(View.VISIBLE);
            // write your more code here on visible
        }
    }
}

答案 5 :(得分:1)

尝试以下代码。评论将引导您完成它:

grayScaleFilterButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        // Show PopupWindow
        showPopup();

        // Everything after this will be handled in `doneInput(String)` method
    }
});

将PopupWindow声明为全局变量:

// Right before onCreate(Bundle)
PopupWindow popupWindow;

在您的活动中创建方法showPopup()

public void showPopup() {

    // Container layout to hold other components    
    LinearLayout llContainer = new LinearLayout(this);

    // Set its orientation to vertical to stack item
    llContainer.setOrientation(LinearLayout.VERTICAL);

    // Container layout to hold EditText and Button
    LinearLayout llContainerInline = new LinearLayout(this);

    // Set its orientation to horizontal to place components next to each other
    llContainerInline.setOrientation(LinearLayout.HORIZONTAL);

    // EditText to get input
    final EditText etInput = new EditText(this);

    // TextView to show an error message when the user does not provide input
    final TextView tvError = new TextView(this);

    // For when the user is done
    Button bDone = new Button(this);

// If tvError is showing, make it disappear 
    etInput.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            tvError.setVisibility(View.GONE);
        }
    });

    // This is what will show in etInput when the Popup is first created
    etInput.setHint("Please provide a number");

    // Input type allowed: Numbers
    etInput.setRawInputType(Configuration.KEYBOARD_12KEY);

    // Center text inside EditText
    etInput.setGravity(Gravity.CENTER);

    // tvError should be invisible at first
    tvError.setVisibility(View.GONE);

    bDone.setText("Done");

    bDone.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // If user didn't input anything, show tvError
            if (etInput.getText().toString().equals("")) {
                tvError.setText("Please enter a valid value");
                tvError.setVisibility(View.VISIBLE);
                etInput.setText("");

            // else, call method `doneInput()` which we will define later
            } else {
                doneInput(etInput.getText().toString());
                popupWindow.dismiss();
            }
        }
    });

    // Define LayoutParams for tvError
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

    layoutParams.topMargin = 20;

    // Define LayoutParams for InlineContainer
    LinearLayout.LayoutParams layoutParamsForInlineContainer = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

    layoutParamsForInlineContainer.topMargin = 30;

    // Define LayoutParams for EditText
    LinearLayout.LayoutParams layoutParamsForInlineET = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

    // Set ET's weight to 1 // Take as much space horizontally as possible      
    layoutParamsForInlineET.weight = 1;

    // Define LayoutParams for Button
    LinearLayout.LayoutParams layoutParamsForInlineButton = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    // Set Button's weight to 0     
    layoutParamsForInlineButton.weight = 0;

    // Add etInput to inline container
    llContainerInline.addView(etInput, layoutParamsForInlineET);

    // Add button with layoutParams // Order is important
    llContainerInline.addView(bDone, layoutParamsForInlineButton);

    // Add tvError with layoutParams
    llContainer.addView(tvError, layoutParams);

    // Finally add the inline container to llContainer
    llContainer.addView(llContainerInline, layoutParamsForInlineContainer);

    // Set gravity
    llContainer.setGravity(Gravity.CENTER);

    // Set any color to Container's background
    llContainer.setBackgroundColor(0x95000000);

    // Create PopupWindow
    popupWindow = new PopupWindow(llContainer, 
                                        ViewGroup.LayoutParams.MATCH_PARENT,
                                             ViewGroup.LayoutParams.WRAP_CONTENT);

    // Should be focusable
    popupWindow.setFocusable(true);

    // Show the popup window
    popupWindow.showAtLocation(llContainer, Gravity.CENTER, 0, 0);

}

最后,doneInput(String)方法:

public void doneInput(String input) {
    int bias = Integer.parseInt(input);

    // Work with it // For example, show a Toast
    Toast.makeText(this, "Number input by user was: " + bias, Toast.LENGTH_LONG).show();

    // Do anything else with input!
}

注意:此版本不需要xml个文件。复制,按照说明操作,然后粘贴即可试用。您可以在屏幕上,任意大小和任何颜色的任何位置显示此PopupWindow

答案 6 :(得分:0)

您可以在xml中通过此行设置输入类型编号:使用此代码,用户只能从软件键盘点击0,1,... 9位数字:

       android:digits="1234567890" or  android:inputType="number"