gpt4 book ai didi

c# - 片段内的膨胀布局

转载 作者:行者123 更新时间:2023-11-30 22:54:38 27 4
gpt4 key购买 nike

我有一个问题。我正在使用 FrameLayout 来使用片段。现在,在我的一个片段中,我想创建一个带有 EditText 的对话框。所以我为我的 fragment.cs 写了这段代码:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.intro_add_new_project, container, false);

ImageButton btnAddProject = view.FindViewById<ImageButton>(Resource.Id.btnAddProject);
btnAddProject.Click += delegate
{
LayoutInflater layoutinflater = LayoutInflater.From(this);
View DialogView = layoutinflater.Inflate(Resource.Layout.setup_project_name_dialog, null);
Android.Support.V7.App.AlertDialog.Builder ProjectNameDialog = new Android.Support.V7.App.AlertDialog.Builder(this);
ProjectNameDialog.SetView(DialogView);

EditText editProjectName = DialogView.FindViewById<EditText>(Resource.Id.editProjectName);
ProjectNameDialog.SetCancelable(false);
ProjectNameDialog.SetPositiveButton("Continue", delegate
{
//My own code here
});
};


return view;
}

现在我遇到了 2x this 的问题。它给出了以下错误:

cannot convert from 'Appname.Fragments.Intro_Add_Project' to 'Android.Content.Context'

我应该填写什么而不是 this。别忘了....这是一个片段!

有人可以帮助我吗?

更新

我得到的错误是:

{Java.Lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at Java.Interop.JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <42dc777b518744fdae9988e94489a4a0>:0 at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <42dc777b518744fdae9988e94489a4a0>:0 at Android.Support.V7.App.AlertDialog+Builder.Show () [0x0000a] in <6bc524e87f8f4e14ab823997c1f60345>:0 at Appname.Fragments.Intro_Add_Project.b__1_0 (System.Object , System.EventArgs ) [0x0005b] in C:\Users\Alexander\source\repos\Appname\Appname\Fragments\Intro_Add_Project.cs:45 --- End of managed Java.Lang.IllegalStateException stack trace --- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:354) at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:83) at android.support.v7.app.AlertController.installContent(AlertController.java:226) at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:260) at android.app.Dialog.dispatchOnCreate(Dialog.java:407) at android.app.Dialog.show(Dialog.java:302) at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:958) at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method) at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30) at android.view.View.performClick(View.java:6669) at android.view.View.performClickInternal(View.java:6638) at android.view.View.access$3100(View.java:789) at android.view.View$PerformClick.run(View.java:26145) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6863) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) }

最佳答案

你应该 LayoutInflater layoutinflater = LayoutInflater.From(Activity);

Android.Support.V7.App.AlertDialog.Builder ProjectNameDialog = new Android.Support.V7.App.AlertDialog.Builder(Activity);

这是我的片段代码。(不要忘记添加ProjectNameDialog.Show();弹出AlertDialog)

     public class Fragment1 : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Create your fragment here
}

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.activity_main, container, false);

ImageButton btnAddProject = view.FindViewById<ImageButton>(Resource.Id.btnAddProject);
btnAddProject.Click += delegate
{
LayoutInflater layoutinflater = LayoutInflater.From(Activity);
View DialogView = layoutinflater.Inflate(Resource.Layout.setup_project_name_dialog, null);
Android.Support.V7.App.AlertDialog.Builder ProjectNameDialog = new Android.Support.V7.App.AlertDialog.Builder(Activity);
ProjectNameDialog.SetView(DialogView);

EditText editProjectName = DialogView.FindViewById<EditText>(Resource.Id.et_name);
ProjectNameDialog.SetCancelable(false);
ProjectNameDialog.SetPositiveButton("Continue", delegate
{
//My own code here
});

ProjectNameDialog.Show();
};
return view;
}
}

有MainActivity.cs

    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.main);
var Fragment1 = new Fragment1();
FragmentManager.BeginTransaction()
.Add(Android.Resource.Id.Content, Fragment1)
.Commit();
}


}

有动态动图

enter image description here

关于c# - 片段内的膨胀布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56067852/

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