Android View中的控件和监听方法...

PS:居然三天没写博客了...今天补上...东西虽多,但是都是一些基础...代码多了一些,有人可能会这样问,粘这么多代码有毛用..其实对于一个Android的初学者来说,一个完整的代码是最容易帮助理解的...我也是在一点一点的去学习...看了许多人的博客,大部分都是粘贴部分代码,说实话,刚接触的时候真的感觉不是很好理解...不知道其他地方如何去实现..只能自己慢慢的去摸索..我写的这东西也不是给大神们去看的...大神一看就能明白怎么回事..因此我是站在初学者的立场上,才贴了这么多的代码...好了,不废话了...

学习内容:

1.基本控件的应用...

2.如何为组件绑定监听事件...

i.Button 与 TextView组件与监听事件 OnClickListener()

在这里我还是多用例子进行讲解,还是比较清晰的...

这是xml文件:

<!--这里定义了四个组件,三个按钮,一个文本显示显示组件..只是一些简单的布局,没什么新的东西,因此也就没有必要讲了,大家看看其实就懂..-->
<?xml version="1.0" encoding="utf-8"?>
<Linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >
    
    <textview
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginbottom="10dp"
        android:text="杜鹃不啼,如何让它啼?"
        android:textsize="20sp" >
    </textview>

    <button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="杀之不足惜!"
        android:textsize="20sp" >
    </button>

    <button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="诱之自然啼!"
        android:textsize="20sp" >
    </button>

    <button
        android:id="@+id/Button03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="待之莫须急!"
        android:textsize="20sp" >
    </button>
</Linearlayout>

下面是src下的MainActivity.java文件..比较简单...没什么难的东西...就是唯一在内部实现的时候大家有可能有点不太理解..

package com.example.android_view;

import android.os.Bundle;
import android.widget.Button;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickLisener; //OnClickListener是View的内部接口..
import android.widget.TextView;
public class MainHelloButton extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // 实现一个多按钮可用的单击监听器对象,这个地方是在内部实现这个方法..只要点击了按钮,那么就会触发这个事件...
        OnClickListener listener = new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTitle("您的答案是:" + ((TextView) v).getText()); //这个地方可以使用setTitle方法将获取的字符串显示在TextView上...
            }
        };

        // 为界面中的每个按钮绑定上这个单击监听器
        findViewById(R.id.Button01).setOnClickListener(listener);
        findViewById(R.id.Button02).setOnClickListener(listener);
        findViewById(R.id.Button03).setOnClickListener(listener);
    }
}

  在这里再说一下TextView,TextView是一个文本显示组件..很常用的一个组件,TextView类被很多的组件类去直接继承或者是间接继承..像Button,EditText,CheckBox,RadioButton等等都是继承了TextView类..TextView是一个非常重要的地方..再举一个例子来深入理解...

<?xml version="1.0" encoding="utf-8"?>

<!--<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">这里也可以在TextView外面加入一层这个东西,也可以直接实现文本滚动..-->
<Linearlayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <textview
        android:id="@+id/TextView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textsize="30sp" >
    </textview>

</Linearlayout>

<!--</ScrollView>-->

java文件...这里就很简单了...别忘我们还需要修改一下string.xml文本...

package android.basic.lesson9;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

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

        // 找到TextView组件
        TextView tv = (TextView) findViewById(R.id.TextView01);

        // 设置移动方法
        tv.setMovementMethod(ScrollingMovementMethod.getInstance());
    }
}

ii.EditText..可编辑的文本显示组件..

布局文件就放这一个控件就可以了...

<?xml version="1.0" encoding="utf-8"?>
<Linearlayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/TextView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textsize="30sp" >
    </EditText>

</Linearlayout>
package android.basic.lesson9;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

public class HelloTextView extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText et = (EditText) findViewById(R.id.EditText01);
        et.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // 监视硬键盘按键
                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
                    //这句话的意思其实就是当我们按下键盘的时候,获取到键盘的的输入值...
                    Toast.makeText(HelloTextView.this, et.getText(),Toast.LENGTH_SHORT).show();
                    // 返回true说明你已经处理了这个事件并且它应该就此终止,如果返回false就表示此事件还需要继续传递下去
                    return true;
                }
                return false;
            }
        });

    }
}

  这个就是JAVA文件了..Toast是信息提示框组件..这里的HelloTextView表示的就是我们主函数,Toast.makeText()表示的是指定显示文本资源和信息的显示时间...大家可以试一下就可以理解这个东西的作用了..

iii.ImageButton和ImageView组件

ImageButton图片按钮...就是一个特殊的按钮而已...ImageView图片视图...二者在被点击的时候所产生的效果是不一样的...这里大家去试试就很容易发现了...

OnTouchListener()与OnClickListener()方法的区别...

<!--在使用图片组件的时候,一定要把图片导入到我们的drawable文件夹下面,否则会报错..-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageButton 
        android:id="@+id/ImageButton01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/p1"
        />
    <ImageView 
        android:id="@+id/ImageView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/p2"/>
</LinearLayout>

java文件。。。

package android.basic.lesson9;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainHelloImageButton extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /*
         * OnClickListener()当屏幕或者是其他组件被点击的时候触发..必须要有点击和松开两个事件..才被触发..
         * OnTouchListener()这个表示的触碰屏幕或者组件的时候就会触发,有可能只是划过某一位置..就被触发..
* 这就二者的区别所在...
*/ // 找到xml中的ImageButton和ImageView final ImageButton ib = (ImageButton) findViewById(R.id.ImageButton01); final ImageView iv = (ImageView) findViewById(R.id.ImageView01); // 定义触摸监听 OnTouchListener otl = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (v.getId()) { case R.id.ImageButton01: Toast.makeText(getApplicationContext(),"触摸" + ((ImageView) v).getId(), Toast.LENGTH_LONG).show(); break; case R.id.ImageView01: Toast.makeText(getApplicationContext(),"触摸" + ((ImageView) v).getId(), Toast.LENGTH_LONG).show(); break; } return false; } }; // 定义点击监听 OnClickListener ocl = new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"点击" + ((ImageView) v).getId(), Toast.LENGTH_LONG).show(); } }; // 绑定监听 ib.setOnClickListener(ocl); ib.setOnTouchListener(otl); iv.setOnClickListener(ocl); iv.setOnTouchListener(otl); } }

iv.CheckBox组件...

复选按钮...复选按钮估计大家也很熟悉,比如说在注册账号的时候就会有这个东西的出现...

OnClickListener()和OnCheckChangedListener()方法的区别...

这里添加了一个文本显示组件和两个复选框..复选框就代表可以一次选择多个...而单选框一次只能选择一个...

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <textview
        android:id="@+id/TextView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="选择你想得到的东西:" >

        <checkbox
            android:id="@+id/CheckBox01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="苹果" >
        </checkbox>

        <checkbox
            android:id="@+id/CheckBox02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="葡萄" >
        </checkbox>
    </textview>

</linearlayout>

Java文件...这段代码说明了一下二者的区别...

package com.example.android_view;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
    private TextView TextView_01;
    private CheckBox CheckBox_01;
    private CheckBox CheckBox_02;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView_1=(TextView)findViewById(R.id.TextView01);
        CheckBox_1=(CheckBox)findViewById(R.id.CheckBox01);
        CheckBox_2=(CheckBox)findViewById(R.id.ChechBox02);
        /*OnClickListener()和OnCheckChangeListener()的区别在于:在复选框中,复选框的值不一定非要通过点击才能够进行选择
         *直接调用setChecked也可以直接进行改变..这样OnClickListener就无法进行监听了..
         * */
        OnClickListener ol=new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(!((CheckBox)arg0).isChecked()){
                    Toast.makeText(MainActivity.this,((CheckBox)arg0).getText()+"被取消",Toast.LENGTH_SHORT).show();
                }
            }
            
        };
        //起初被点击的时候会直接触发这个监听..当其他复选框被选择的时候这个监听也会被触发..因为可以直接通过OnCheckChangeListener进行直接监听..
        OnCheckedChangeListener od=new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if(arg1){
                    Toast.makeText(MainActivity.this,arg0.getText()+"被选择",Toast.LENGTH_SHORT).show();//获取这个对象的资源信息..
                }
                else{
                    Toast.makeText(MainActivity.this, arg0.getText()+"被取消", Toast.LENGTH_SHORT).show();
                }
                
            }
        };
        CheckBox_1.setOnClickListener(ol);
        CheckBox_2.setOnClickListener(ol);
        CheckBox_1.setOnCheckedChangeListener(od);
        CheckBox_2.setOnCheckedChangeListener(od);
}
  
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
   
}

v.RadioGroup和RadioButton

这两个代表单选组和单选按钮..这里我们在单选组中加入了两个单选按钮子元素...很简单的东西....

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <radiogroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <radiobutton
            android:id="@+id/radio_red"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="红" >
        </radiobutton>
            <radiobutton
                android:id="@+id/radio_blue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="蓝" >
            </radiobutton>
        
    </radiogroup>

</linearlayout>
package android.basic.lesson9;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.Toast;

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

                final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
                final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);

                OnClickListener ocl = new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                        Toast.makeText(MainHelloRadioGroup.this, ((RadioButton)v).getText(), Toast.LENGTH_SHORT).show();

                        }
                };

                radio_red.setOnClickListener(ocl);
                radio_blue.setOnClickListener(ocl);
        }
}

这一章内容虽然很多,但是也就是一些基础的东西...主要还是代码多了一些,方便理解..组件这东西多练习练习其实就很容易掌握的...

原文地址:https://www.cnblogs.com/RGogoing/p/4531572.html