/layout/activity_main.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:id="@+id/mytext" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/mybtn" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </RelativeLayout>
com.example.demo01textviewbutton.MainActivitypackage com.example.demo01textviewbutton; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; //当在/layout/activity_main.xml中加控件后,R会自动生成相应的资源文件ID public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //编写相应业务逻辑 TextView textView = (TextView) super.findViewById(R.id.mytext); textView.setText("buy big house"); Button mybutton=(Button)super.findViewById(R.id.mybtn); mybutton.setText("clink me ,xixihehehaha"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
R.java中生成以下: public static final class id { public static final int action_settings=0x7f080002; public static final int mybtn=0x7f080001; public static final int mytext=0x7f080000; }
时间: 2024-10-14 12:46:08