从操作栏菜单项捕获用户输入

时间:2017-02-18 20:17:28

标签: java android xml

我正在Android Studio中创建一个健身房应用程序,我想要实现的第一个功能是让用户通过单击操作栏中的选项来添加它来创建锻炼。单击此按钮将显示一个带有EditText字段的警告对话框,以输入锻炼名称。稍后,我将使用输入创建添加了不同训练的列表视图,但是现在我只关心从此EditText字段捕获输入。

以下是应该发生的事情..在this screen上点击+按钮,它会弹出一个带有EditText字段的警告对话框。我想在java主活动文件中捕获此input

这是java MainActivity文件。我希望EditText字段的输入存储在m_Text变量中。

public class MainActivity extends AppCompatActivity {

private String m_Text;

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
//Clicking add workout button in the action bar
//stackoverflow.com/questions/13143006/alert-dialog-from-within-onooptionsitemselected-android
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()){
        case R.id.action_add_workout:
            //final EditText mAddWorkout = (EditText)R.layout.userinput;

            //Creating the dialog box for entering the workout name
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Enter the workout name");

            //Create the user input xml file into a java object; capturing the user input from the dialog box
            //inflate means "fill"
            View view = LayoutInflater.from(this).inflate(R.layout.userinput,null);
            final EditText mAddWorkout = (EditText)view.findViewById(R.id.workout_name_input);

            builder.setView(R.layout.userinput);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    m_Text = mAddWorkout.getText().toString();
                    boolean brkpt = true;
                }
            }); //Second parameter pass in which event listener should trigger when the button is clicked
            builder.setNegativeButton("Cancel",null);
            builder.show();

            return true;

        default:
            return super.onOptionsItemSelected(item);
      }
     }
   }

以下是我的操作栏菜单项的xml,用于添加锻炼名称(main_menu.xml)

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_add_workout"
    android:icon="@drawable/ic_add"
    android:title="@string/add_workout"
    app:showAsAction="always"/>
</menu>

最后,EditText的xml(userinput.xml)

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/workout_name_input"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:hint="name..">
</EditText>    

当我调试代码时,如果我在EditText字段中输入锻炼名称,则m_Text变量始终为空。我已经被困在这几天了,我已经梳理了youtube和SO以获得答案,并且没有找到与我的问题有关的内容。 非常感谢任何见解。谢谢。

编辑:更新了MainActivity的代码。我可以将控制传递给自定义点击器,但输入仍未保存。感谢

 public class MainActivity extends AppCompatActivity {

private EditText mAddWorkout;

public class CustomClickListener implements View.OnClickListener {

    private final Dialog dialog;

    CustomClickListener(Dialog dialog) {
        this.dialog = dialog;
    }

    @Override
    public void onClick(View v) {
        String editTextValue= mAddWorkout.getText().toString();
    }
}

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
//Clicking add workout button in the action bar
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()){
        case R.id.action_add_workout:
            //final EditText mAddWorkout = (EditText)R.layout.userinput;

            //Creating the dialog box for entering the workout name
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Enter the workout name");

            //Create the user input xml file into a java object; capturing the user input from the dialog box
            //inflate means "fill"
            View view = LayoutInflater.from(this).inflate(R.layout.userinput,null);
            mAddWorkout = (EditText)view.findViewById(R.id.workout_name_input);

            builder.setView(R.layout.userinput);
            builder.setPositiveButton("OK",null);
            builder.setNegativeButton("Cancel",null);

            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            Button saveWorkout = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
            saveWorkout.setOnClickListener(new CustomClickListener(alertDialog));
            builder.show();

            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
  }
}

******最终编辑******* 我已经完成整个应用程序,并认为我会发布我的警报对话框代码,以防它帮助其他人。事实证明,这个问题与警报对话框是我唯一的主要问题,一旦我习惯了android studio和java的东西真的起飞了。无论如何我的gymapp是一个很好的小应用程序,使用SQLlite来跟踪锻炼,练习和设置..我实际上在健身房使用它:))

@Override
//Clicking add workout button in the action bar
public boolean onOptionsItemSelected(MenuItem item) {

    //Creating the dialog box for entering the workout name
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    final EditText input = new EditText(this);

    builder.setTitle("Enter the workout name").setView(input).setView(input);

    builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Workout workout = new Workout(input.getText().toString());
            long workout_key = myDb.createWorkout(workout);
            populateWorkouts();
        }
    });
    builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.show();

    return super.onOptionsItemSelected(item);
}

就我而言,我使用输入创建一个锻炼对象并将其插入我的数据库。显然,如果操作栏菜单中有多个选项,则需要一个switch语句。

再次感谢那些试图帮助我的2个人。

2 个答案:

答案 0 :(得分:1)

您应该如何执行此操作:

创建客户点击监听器

private class CustomClickListener implements View.OnClickListener {

    private final Dialog dialog;

    CustomClickListener(Dialog dialog) {
        this.dialog = dialog;
    }

    @Override
    public void onClick(View v) {
        String editTextValue= mAddWorkout.getText().toString();
    }
}

您应该设置mAddWorkout值类级别,以便轻松访问它!

然后:

将此点击监听器设置为您的对话框

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Enter the workout name");

        //Create the user input xml file into a java object; capturing the user input from the dialog box
        //inflate means "fill"
        View view = LayoutInflater.from(this).inflate(R.layout.userinput,null);
        final EditText mAddWorkout = (EditText)view.findViewById(R.id.workout_name_input);

        builder.setView(R.layout.userinput);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                boolean brkpt = true;
            }
        }); //Second parameter pass in which event listener should trigger when the button is clicked
        AlertDialog alertDialog = builder.create();
        alertDialog .show();
Button saveWorkout = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
    saveWorkout .setOnClickListener(new CustomClickListener(alertDialog));

这是我用于自己的代码并且工作正常的解决方案;你可以在点击监听器的onClick方法中进行验证,并相应地提醒用户!

祝你好运!

答案 1 :(得分:1)

private android.app.AlertDialog mAlert;
private EditText mAddWorkout;

//Creating the dialog box for entering the workout name
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = LayoutInflater.from(this).inflate(R.layout.userinput,null);

builder.setTitle("Enter the workout name");
mAlert = builder.create();
mAlert.setCancelable(true);
mAlert.setView(view, 10, 10, 10, 10);
if (mAlert != null && !mAlert.isShowing()) {
                mAlert.show();
            }
mAddWorkout = (EditText) mAlert.findViewById(R.id.workout_name_input);
m_Text = mAddWorkout.getText().toString();

希望这能解决您的问题