<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="78dp" android:text="TextView" />
import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.TextView; import android.widget.Button; import android.widget.Toast;
import android.widget.TextView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView editT1 = (TextView)findViewById(R.id.editText1); } }
<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" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:padding="@dimen/padding_medium" android:text="@string/hello_world" tools:context=".MainActivity" /> </RelativeLayout>
android:text="@string/hello_world"
<resources> <string name="app_name">MyApp</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">My App V 1.0</string> </resources>
package com.myapp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.support.v4.app.NavUtils; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Bundle savedInstanceState เป็น Object ของ import android.os.Bundle; ทำหน้าที่จัดการ Resource และสถานะของโปรแกรม setContentView(R.layout.activity_main); // โหลด Layout ของ activity_main.xml มาแสดง }
public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); // กรณีที่ใน activity_main.xml มี XML ที่เกี่ยวกับ Menu ก็จะโหลดเมนูเหล่านั้นไปแสดงด้วย return true; }
TextView txtView; txtView = (TextView)findViewById(R.id.textView1);
TextView txtView; txtView = (TextView)findViewById(R.id.textView1); txtView.setText("Welcome to www.ThaiCreate.Com"); txtView.setTextColor(Color.RED);
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="93dp" android:padding="@dimen/padding_medium" android:text="@string/hello_world" tools:context=".MainActivity" />
package com.myapp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.support.v4.app.NavUtils; import android.widget.TextView; import android.graphics.Color; import android.view.View; import android.app.AlertDialog; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Dialog final AlertDialog.Builder adb = new AlertDialog.Builder(this); // textView1 TextView txtView; txtView = (TextView)findViewById(R.id.textView1); txtView.setText("Welcome to www.ThaiCreate.Com"); txtView.setTextColor(Color.RED); txtView.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click // textView2 TextView txtView2; txtView2 = (TextView)findViewById(R.id.textView2); txtView2.setText("Hello www.ThaiCreate.Com"); // Show Dialog AlertDialog ad = adb.create(); ad.setMessage("Hello www.ThaiCreate.Com"); ad.show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
<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" > </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.support.v4.app.NavUtils; import android.widget.TextView; import android.graphics.Color; import android.view.View; import android.app.AlertDialog; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); // Create txtView1 TextView txtView1 = new TextView(this); txtView1.setText("Welcome to www.ThaiCreate.Com"); txtView1.setTextColor(Color.RED); setContentView(txtView1); final AlertDialog.Builder adb = new AlertDialog.Builder(this); txtView1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click // Show Dialog AlertDialog ad = adb.create(); ad.setMessage("Hello www.ThaiCreate.Com"); ad.show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท