Hus*_*ary 2 android android-layout xamarin
主要活动我正在命名我的项目"MyFirstApp"
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace MyFirstApp
{
[Activity (Label = "MyFirstApp", MainLauncher = true)]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
button.Text = string.Format ("{0} clicks!", count++);
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
布局 res /布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter Message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/button_send" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
请帮助我,我是android开发的初学者, 如果有什么不清楚的话,我会尝试添加更多细节,我正在使用Xamarin进行开发
谢谢
添加属性:
android:id="@+id/myButton"
Run Code Online (Sandbox Code Playgroud)
到布局xml文件中的按钮.
然后在您的活动中,您可以找到如下按钮:
Button button = (Button) findViewById(R.id.myButton);
Run Code Online (Sandbox Code Playgroud)
代替:
Button button = FindViewById<Button> (Resource.Id.myButton);
Run Code Online (Sandbox Code Playgroud)
更新:嗯,我提到的findViewById是普通的Android,不确定它在Xamarin中是如何工作的,但是肯定你的按钮在xml中需要一个id
更新2:实际上你已经拥有了Id和你的按钮ID,但它不是myButton,它是button_send
==>所以你可能想要使用那个而不是myButton
归档时间: |
|
查看次数: |
19592 次 |
最近记录: |