android onClickListener 和 setOnClickListener





HelloActivity.java:


package com.example.android.helloactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class HelloActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
find_and_modif_button();
}

private void find_and_modif_button()
{
//定义button
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(button_listener);

//定义text_view_button
Button text_view_button = (Button)findViewById(R.id.text_view_button);
text_view_button.setOnClickListener(text_view_button_listener);

//定义edit_view_button
Button edit_view_button =(Button)findViewById(R.id.edit_view_button);
edit_view_button.setOnClickListener(edit_view_button_listener);
}

//定义button的点击响应方法
private Button.OnClickListener button_listener =new Button.OnClickListener(){
public void onClick(View v){
setTitle("哎哟,button被点了一下");
}
};

//定义text_view_button的点击响应方法
private Button.OnClickListener text_view_button_listener = new Button.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent();
intent.setClass(HelloActivity.this,TextViewActivity.class);
startActivity(intent);
}
};

//定义edit_view_button的点击响应方法
private Button.OnClickListener edit_view_button_listener =new Button.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent();
intent.setClass(HelloActivity.this,EditViewActivity.class);//要关联到EdirTextActivity.java,则需新建一个
startActivity(intent);
}

};
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="这是button"/>
<Button
android:id="@+id/text_view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
<Button
android:id="@+id/edit_view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditView"/>
</LinearLayout>


EditViewActivity.java:

package com.example.android.helloactivity;
//import com.android.button.R;
import android.app.Activity;
//import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class EditViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("ViewViewActivity");
setContentView(R.layout.editview);
find_and_modify_text_view();
}
//定义get_edit_button
private void find_and_modify_text_view() {
Button get_edit_view_button = (Button)findViewById(R.id.get_edit_view_button);
get_edit_view_button.setOnClickListener(get_edit_view_button_listener);
}
//定义get_edit_view_button的点击响应方法
private Button.OnClickListener get_edit_view_button_listener =new Button.OnClickListener(){
public void onClick(View v){
EditText edit_text =(EditText)findViewById(R.id.edit_text);
CharSequence edit_text_value =edit_text.getText();
setTitle("EditText的值:"+edit_text_value);
}
};

}

editview.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/edit_text"
android:text="这里可以输入内容"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<Button
android:id="@+id/get_edit_view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取EditView的值">
</Button>

</LinearLayout>


TextViewActivity.java


package com.example.android.helloactivity;
//import com.android.button.R;
import android.app.Activity;
//import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TextViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setTitle("ViewViewActivity");
setContentView(R.layout.textview);

find_and_modify_text_view();
}

//定义get_text_button
private void find_and_modify_text_view() {
Button get_text_view_button = (Button)findViewById(R.id.get_text_view_button);
get_text_view_button.setOnClickListener(get_text_view_button_listener);
}

//定义get_text_view_button的点击响应方法
private Button.OnClickListener get_text_view_button_listener =new Button.OnClickListener(){
public void onClick(View v){
TextView text_view =(TextView)findViewById(R.id.text_view);
CharSequence text_view_value =text_view.getText();
setTitle("TextView的值:"+text_view_value);
}
};

}


textview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text_view"
android:text="这里text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<Button
android:id="@+id/get_text_view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取TextView的值">
</Button>

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.helloactivity">
<application android:label="Hello, Activity!">
<activity android:name="HelloActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<!-- 新建了EditViewActivity.java文件,则要添加这句<activity android:name="EditViewActivity"></activity> -->
<activity android:name="EditViewActivity">
</activity>
<activity android:name="TextViewActivity">
</activity>
</application>
</manifest>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值