更多精彩内容,请点击阅读:《API Demos 2.3 学习笔记》
Android中对于Button控件的监听方法主要有两种:
1、设置监听器
通过设置监听器来监听用户对于按钮的点击响应。当用户点击该按钮时,便会触发监听器,并执行监听器中onClick方法内部定义的指定动作。
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 点击触发时需要执行的动作
}
});
2、自定义监听方法
首先需要在Layout布局文件中为该按钮添加属性(android:onClick="selfDestruct" )。其中selfDestruct为自定义监听方法名称,后面需要用到。
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/self_destruct"
android:onClick="selfDestruct" />
接着,在Activity中实现自定义监听方法:
public void selfDestruct(View view) {
// 点击触发时需要执行的动作
}
注: 1、该自定义方法必须是Public类型;
2、该自定义方法必须并且只能接受一个参数View。
下面我们进行实例代码解析:
res-value-string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="log_text_box_1_do_nothing_text">Do nothing</string>
<string name="log_text_box_1_add_text">Add</string>
</resources>
res-layout-log_text_box_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- android:onClick="selfDestruct" 监听方法二需要添加此属性,其中selfDestruct为自定义监听方法名称-->
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/log_text_box_1_add_text"
/>
<!--自定义控件 LogTextBox ,继承自 TextView -->
<com.example.android.apis.text.LogTextBox
android:id="@+id/text"
android:background="@drawable/box"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:scrollbars="vertical"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/log_text_box_1_do_nothing_text"/>
</LinearLayout>
src-com.example.android.apis.text-LogTextBox.java
package com.example.android.apis.text;
import android.widget.TextView;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.text.method.MovementMethod;
import android.text.Editable;
import android.util.AttributeSet;
/**
* 这是一个可以编辑并且默认可以滚动的TextView控件。
* 类似缺少光标的EditText控件。
*
* <p>
* <b>XML attributes</b>
* <p>
* See
* {@link android.R.styleable#TextView TextView Attributes},
* {@link android.R.styleable#View View Attributes}
*/
public class LogTextBox extends TextView {
public LogTextBox(Context context) {
this(context, null);
}
public LogTextBox(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}
public LogTextBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected MovementMethod getDefaultMovementMethod() {
return ScrollingMovementMethod.getInstance();
}
@Override
public Editable getText() {
return (Editable) super.getText();
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, BufferType.EDITABLE);
}
}
src-com.example.android.apis.text-LogTextBox1.java
package com.example.android.apis.text;
import com.example.android.apis.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class LogTextBox1 extends Activity {
private LogTextBox mText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_text_box_1);
mText = (LogTextBox) findViewById(R.id.text);
//对Button的监听方法一:设置监听器
Button addButton = (Button) findViewById(R.id.add);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mText.append("This is a test\n");
} });
}
//对Button的监听方法二:自定义监听方法,需要设置android:onClick属性
//这个方法必须是Public类型的,并且只能接受view作为唯一参数
public void selfDestruct(View view) {
mText.append("This is a test\n");
}
}
知识点1:android:text="@string/log_text_box_1_add_text"
在android中,建议将所有固定字符串资源放在res/values/string.xml文件中,方便统一管理。在layout布局文件等xml类型的文件中引用字符串资源时,通过"@string/字符串资源名称"格式进行引用。而在Java代码中需要引用字符串资源时,则通过格式”R.string.字符串资源名“进行引用,具体引用方法如下所示:
//通过string字符串资源获得CharSequence
CharSequencecs = getText(R.string.log_text_box_1_add_text);
//获取字符串资源,并且显示在TextView控件上
mText= (TextView) findViewById(R.id.text);
mText.setText(R.string.log_text_box_1_add_text);
知识点2:android:scrollbars="vertical"
该属性定义控件在滚动时是否显示滚动条。该属性可以同时取多个值,但必须用”|”隔开。例如:横向纵向都支持滚动条显示(android:scrollbars="horizontal|vertical")
常量 | 值 | 描述 |
---|---|---|
none | 0x00000000 | 不显示滚动条 |
horizontal | 0x00000100 | 仅仅现实横向滚动条 |
vertical | 0x00000200 | 仅仅现实纵向滚动条 |
知识点3:android:layout_weight="1"
layout_weight用于给一个线性布局中的诸多视图的重要度赋值。所有的视图都有一个layout_weight值,默认为零,意思是需要显示多大的视图就占据多大的屏幕空间。若赋一个高于零的值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight值以及该值在当前屏幕布局的整体layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。
举个例子:比如说我们在水平方向上有一个文本标签和两个文本编辑元素。该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,则剩余空间的三分之一分给第一个,三分之而分给第二个(数值越大,重要度越高)。
关于layout_weight更完整的解释,请参考以下文章:http://blog.csdn.net/jincf2011/article/details/6598256
注:值得注意的是,在水平布局中设置layout_weight的时候,必须这样进行设置android:layout_width="0dip"。同理,在垂直布局中设置layout_weight时,也必须要做相应设置android:layout_height="0dip"。
效果预览:
欢迎大家关注我的微信公众号:
微信公众号:sn0wdr1am