gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:02:01 25 4
gpt4 key购买 nike

我正在 Android Studio 中创建一个健身房应用程序,我尝试实现的第一个功能是让用户通过单击操作栏中的选项来添加锻炼来创建锻炼。单击此按钮会弹出一个警报对话框,其中包含一个用于输入锻炼名称的 EditText 字段。稍后,我将使用输入创建一个 ListView ,其中添加了不同的锻炼,但现在我只关心从此 EditText 字段捕获输入。

这就是 this screen 上应该发生的事情。我单击 + 按钮,它会弹出一个带有 EditText 字段的警报对话框。我想捕捉这个input在java主 Activity 文件中。

这是 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 语句。

再次感谢两位试图帮助我的人。

最佳答案

您应该如何执行此操作:

创建客户点击监听器

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 方法中进行验证并相应地提醒用户!

祝你好运!

关于java - 从操作栏菜单项捕获用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42320225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com