gpt4 book ai didi

java - 如何获取在另一个类中以编程方式创建的 View ID?

转载 作者:行者123 更新时间:2023-12-01 16:18:22 24 4
gpt4 key购买 nike

我想查找从另一个类中的可运行集以编程方式创建的 View 的 ID,并将其传递给搜索到的 View 的类。

这个想法是只要没有输入正确的密码就锁定应用程序。因此,当用户在主菜单上按下“解锁”按钮时,会提示 ViewDialog 类中的覆盖对话框。ViewDialog 中的 showUnlockDialog() 是在 R.layout.custom_dialog 中创建密码 EditText 并将其添加到基本对话框集的函数。用户输入密码,然后单击运行可运行的按钮来获取 EditText 内容以检索输入的密码。

public class MainMenu extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {

....

Runnable runnable;
View v;

runnable = new Runnable(){
public void run(){
Runnable runnable = new Runnable(){
public void run(){
EditText mdp = findViewById((int)1);
Log.d("test", mdp.getText().toString());
}
};
ViewDialog alert = new ViewDialog();
alert.showUnlockDialog(MainMenu.this, runnable);
}
};

v = findViewById(R.id.button);
v.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
runnable.run();
}
});

....
}
}

public class ViewDialog extends AppCompatActivity{
Button positiveButton;
Button negativeButton;

....

public void showUnlockDialog(Activity activity, Runnable callback){
final Dialog dialog = new Dialog(activity);
dialog.setCancelable(true);
dialog.setContentView(R.layout.custom_dialog);

....

ConstraintLayout constraintLayout = new ConstraintLayout(activity);
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
constraintLayout.setLayoutParams(params);

EditText password = new EditText(activity);
password.setId((int)1);

constraintLayout.addView(password);

LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.customDialogLinLayout);
layout.addView(constraintLayout);

setDialog1Button(activity, dialog, callback);

dialog.show();
}

public void setDialog1Button(Activity activity, final Dialog dialog, final Runnable callback){
positiveButton = new Button(activity);
positiveButton.setText("OK");

positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(callback != null) {
callback.run();
}
dialog.dismiss();
}
});

LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.customDialogLinLayout);
layout.addView(positiveButton);
}

....
}

点击验证按钮时,密码为空

    EditText password = findViewById((int)1);
Log.d("test", password.getText().toString());

并提示如下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.megacom.inventaire, PID: 18759
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.megacom.inventaire.MainMenu$1$1.run(MainMenu.java:55)
at com.megacom.inventaire.ViewDialog$1.onClick(ViewDialog.java:107)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

最佳答案

您可以使用 Intent 传递该 View ,也可以将该 View 设为常量以更改其他 Activity 或类上的 View 值

关于java - 如何获取在另一个类中以编程方式创建的 View ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62341228/

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