简体   繁体   English

从EditText输入和打印

[英]Input and printing from EditText

So I'm trying to get a string from an EditText view and store into a variable which I will then print when the CreateBtn is clicked. 因此,我试图从EditText视图中获取一个字符串并将其存储到一个变量中,然后在单击CreateBtn时将其打印出来。 Cant quite get this to work. 不能让它正常工作。 Keeps crashing, I think, because of the CreateBtn and I think it might have something to do with the AlertBox which is where the EditText view is located. 由于CreateBtn,我一直崩溃,我认为它可能与EditText视图所在的AlertBox有关。

public class MyActivity extends Activity {


// declares vars
final Context context = this;
private Button addClassBtn;
private Button createBtn;
public EditText classNameInput;
private String classTextName;

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

    // initializes var
    final LayoutInflater classInfo = getLayoutInflater();
    addClassBtn = (Button) findViewById(R.id.classBtn);
    classNameInput = (EditText) findViewById(R.id.classNameInput);
    createBtn = (Button) findViewById(R.id.createBtn);

    // creates a alert box with a text view when button is clicked
    addClassBtn.setOnClickListener(
        new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder builder = new AlertDialog.Builder(context);

            builder.setView(classInfo.inflate(R.layout.class_info, null));

            builder.setTitle("Add Class");

            AlertDialog diaBox = builder.create();

            diaBox.show();

        }
    });

    //classNameInput is extracted to a variable
    createBtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String className;

                    className = classNameInput.getText().toString();

                    Log.v("EditText", className);

                }
            });

Here is class_info.xml 这是class_info.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
    android:id="@+id/classNameInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:paddingTop="25dp"
    android:hint="    class name    "
    android:textSize="25sp"
    android:paddingBottom="20dp"
    android:fontFamily="monospace"
    android:inputType="textAutoComplete|textAutoCorrect"
    android:textAlignment="center"
    android:paddingLeft="-10dp"
    android:paddingRight="-10dp" />

<Button
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/classNameInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/createBtn"
    android:fontFamily="monospace"
    android:textSize="23sp"
    android:text="create"
    android:onClick="createBtnClick" />

</RelativeLayout>

Here is activity_my.xml 这是activity_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"
    android:id="@+id/classBtn"
    android:clickable="false"
    android:textSize="100sp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="onClassBtnClick"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:focusable="true" />

 </RelativeLayout>

I think you got a NullPointerException when you press the createBtn because your classNameInput is null when you trying to use it in the OnClickListener of your createBtn. 我认为当您按下createBtn时会收到NullPointerException,因为当您尝试在createBtn的OnClickListener中使用它时,classNameInput为null。

The statement " (EditText) findViewById(R.id.classNameInput) " always return null because your EditText is located in the AlterDialog which is not created yet when you use that statement. 语句“ (EditText) findViewById(R.id.classNameInput) ”始终返回null,因为您的EditText位于使用该语句时尚未创建的AlterDialog中。

In order to solve your problem, I need to know where your createBtn is. 为了解决您的问题,我需要知道您的createBtn在哪里。 It's better if you post your activity_my xml and class_info xml. 最好张贴您的activity_my xml和class_info xml。

Update: 更新:

According to layout files you post, I know that you got a NullPointerException immediately when you start your MyActivity because your createBtn is null when you set a OnClickListener to it. 根据您发布的布局文件,我知道您在启动MyActivity时会立即收到NullPointerException,因为将OnClickListener设置为createBtn时为null。 The reason is the same as what I said about your classNameInput. 原因与我对您的classNameInput所说的相同。

I changed part of you code to solve your problem. 我更改了部分代码以解决您的问题。 The following code should fix your problem: 以下代码可以解决您的问题:

public class MyActivity extends Activity {


// declares vars
final Context context = this;
private Button addClassBtn;
private Button createBtn;
public EditText classNameInput;
private String classTextName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
       // initializes var
    final LayoutInflater classInfo = getLayoutInflater();
    addClassBtn = (Button) findViewById(R.id.classBtn);

    // creates a alert box with a text view when button is clicked
    addClassBtn.setOnClickListener(
        new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder builder = new AlertDialog.Builder(context);

            View dialogView = classInfo.inflate(R.layout.class_info, null);

            setDialogViews(dialogView);

            builder.setView(dialogView);

            builder.setTitle("Add Class");

            AlertDialog diaBox = builder.create();

            diaBox.show();

        }
    });


    private void setDialogViews(View dialogView){
        classNameInput = (EditText) dialogView.findViewById(R.id.classNameInput);
        createBtn = (Button) dialogView.findViewById(R.id.createBtn);

        //classNameInput is extracted to a variable
        createBtn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String className;

                        className = classNameInput.getText().toString();

                        Log.v("EditText", className);

                    }
                });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM