[英]Unable to see new activity
我的意思是制作一個帶有“設置”按鈕的應用程序,按下該按鈕將創建一個新的活動。 我希望該新活動顯示實際設置頁面。
以下是“設置”按鈕的XML代碼:
<Button
android:text="Settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonResSettings" />
這是我稱為新活動的代碼,該代碼位於主活動的OnCreate
:
Button resSetButton = FindViewById<Button>(Resource.Id.buttonResSettings);
resSetButton.Click += delegate {
var ResSetAct = new Intent(this, typeof(ResSettingsActivity));
ResSetAct.PutExtra("res", res);
StartActivity(ResSetAct);
};
這是我的新活動的代碼:
public class ResSettingsActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set the view to ResSettings(.axml)
SetContentView(Resource.Layout.ResSettings);
//Fetch the data of the variables
string res = Intent.GetStringExtra("res");
//Map the elements of the view to usable objects
EditText CurRes = FindViewById<EditText>(Resource.Id.editCurRes);
//Display the current values of the settings
CurRes.Text = res;
}
}
在仿真器中構建和使用應用程序的其余部分沒有問題,但是當我單擊該“設置”按鈕時,沒有任何反應(除了默認的onClick按鈕設計更改)。
您需要記住將[Activity]
屬性添加到新的Activity類中:
[Activity(Label = "Res Settings")]
public class ResSettingsActivity : Activity
{
...
}
該屬性將在構建時為您在AndroidManifest.xml中添加一個條目。 您可以通過查看obj / Debug / android文件夾中的結果清單來驗證這一點。 您應該看到類似以下內容的條目:
<activity android:label="Res Settings" android:name="md51237869127761dfsa77sadvfb.ResSettingsActivity" />
如果您不希望名稱以md5作為前綴,則可以將Name
屬性添加到Activity Attribute:
[Activity(Label = "Res Settings", Name = "my.package.name.ResSettingsActivity")]
Intent theintent = new Intent(A.this,B.java);
theintent.putExtra("name",john);
startActivity(theintent);
還要在清單文件中聲明類b.java
。
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
// Set the request code to any code you like, you can identify the
// callback via this code
startActivity(i);
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
您可以傳遞這樣的意圖,並在清單文件中聲明類DisplayMessageActivity
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.