Log.d(TAG_NAME,TEXT_MESSAGE);
<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/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="18dp" android:textAppearance="?android:attr/textAppearanceLarge" android:text="i = 0" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="42dp" android:text="Button1 (i = i + 1)" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="25dp" android:text="Button2 (i = i + 2)" /> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.app.Activity; import android.util.Log; public class MainActivity extends Activity { int i =0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // textView1 final TextView txtView1 = (TextView) findViewById(R.id.textView1); // button1 (i = i + 1) final Button btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { i = i + 1; Log.d("Step 1","i = " + i); txtView1.setText("i = " + i); // Show on textView1 } }); // button2 (i = i + 2) final Button btn2 = (Button) findViewById(R.id.button2); btn2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { i = i + 2; Log.d("Step 2","i = " + i); txtView1.setText("i = " + i); // Show on textView2 } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท