Button的onClick属性用法:
Layout:
<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="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/txtViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:gravity="center"
android:text="@string/hello_world" />
<Button
android:id="@+id/btnId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OnClick~"
android:onClick="On_Click"/>
</LinearLayout>
activity:
package com.away.btnonclick;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView txtViewId;
private Button btnId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtViewId=(TextView)findViewById(R.id.txtViewId);
btnId=(Button)findViewById(R.id.btnId);
}
public void On_Click(View v){
txtViewId.setText("hello world!");
}
}
欢迎交流 http://blog.csdn.net/ycwol/article/details/39250735