0

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

我想让用户在单击按钮时被提示输入integer input ONLYTextField ,并且我不希望等在用户单击按钮之前显示在屏幕上。用户输入必须保存在int bias.

任何帮助,将不胜感激。

4

7 回答 7

1

无论textField您在说什么,您都可以设置输入类型,例如:

textFieldName.setRawInputType(Configuration.KEYBOARD_12KEY);

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

android:inputType="number"

此外,即使您创建了 TextField,也可以使用以下命令将可见性设置为不可见:

textFieldName.setVisibility(View.INVISIBLE);

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

textFieldName.setVisibility(View.VISIBLE);

正如您在问题中提到的那样,您可以更改按钮单击的可见性。

于 2013-09-02T04:31:07.827 回答
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 ExamplemDialog.show()在按钮单击侦听器中使用。

这里mDialog是对象Dialog

于 2013-09-02T04:32:14.463 回答
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
        }
    }
}
于 2013-09-02T04:55:44.823 回答
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在屏幕上的任何位置、任何尺寸和任何颜色显示它。

于 2013-09-02T05:35:24.250 回答
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

        }


        }
    });
于 2013-09-02T04:40:14.577 回答
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;
    }

}
于 2013-09-02T04:48:24.190 回答
0

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

       android:digits="1234567890" or  android:inputType="number"
于 2013-09-02T04:53:07.783 回答