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>
android onClickListener 和 setOnClickListener
最新推荐文章于 2021-10-25 10:44:30 发布