Android开发学习笔记(三):Button单击事件实现方法的总结(新增圆角按钮实现方式)

本章学习笔记介绍Button事件实现的两种方法

在界面中直接拉取创建button按钮 Graphical layout -> Form Widgets -> button (这里我创建了两个按钮用于区分)

创建好界面的xml界面代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testone.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="33dp"
        android:text="按钮2" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="42dp"
        android:text="按钮1" />
    
</RelativeLayout>

注:我本是做.net开发,有前台和后台之分,我也已经习惯这样叫,也同意区分。就把界面部分代码就做前台也就是xml部分,后台则是java部分。

 

前台代码OK,接下来就是后台触发按钮事件的代码。

第一种:

首先我们要引用按钮的一些类。

import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.Toast;  

这些类具体有啥用,可以查阅一下API,这里我就不逼逼了。

然后写代码寻找前台界面中的按钮并响应按钮的单击事件。

public class MainActivity extends Activity {
    Button mybutton1,mybutton2;  //创建两个按钮的全局变量
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mybutton1 =(Button)findViewById(R.id.button1);  //寻找界面中按钮1
        mybutton2 =(Button)findViewById(R.id.button2);  //寻找界面中按钮2       
mybutton1.setOnClickListener(
new ButtonClick()); //赋予按钮1单击动作产生的事件 mybutton2.setOnClickListener(new ButtonClick()); //赋予按钮2单击动作产生的事件  }
//创建一个类,来响应OnClickListener class ButtonClick implements OnClickListener { public void onClick(View v) { switch (v.getId()) { case R.id.button1: Toast.makeText(MainActivity.this, "你点击了按钮1",Toast.LENGTH_LONG).show(); //单击按钮1提示  break; case R.id.button2: Toast.makeText(MainActivity.this, "你点击了按钮2",Toast.LENGTH_LONG).show(); //单击按钮2提示
          break; default: break; } } } @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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }


注:Toast类中有许多用法,这里只是用了一种提示信息的方法,想更多了解这个类可以百度一下。

 

第二种:

import android.app.Activity;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.Toast;  
 
public class ButtonDemoActivity extends Activity {  
    Button myButton1,myButton2;  
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        myButton1=(Button)findViewById(R.id.myButton1);  
        myButton2=(Button)findViewById(R.id.myButton2);  
          
        //使用匿名类注册Button事件  
        myButton1.setOnClickListener(new OnClickListener()  
        {         
            public void onClick(View v)  
            {  
                Toast.makeText(ButtonDemoActivity.this, "你点击了按钮1",Toast.LENGTH_LONG).show();  
            }  
        });  
        myButton2.setOnClickListener(new OnClickListener()  
        {         
            public void onClick(View v)  
            {  
                Toast.makeText(ButtonDemoActivity.this, "你点击了按钮2",Toast.LENGTH_LONG).show();  
            }  
        });  
    }  
} 

第二种转自IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/629329

第二种我没有验证过,第一种我是亲自完成实例的。


最后button按钮单击事件使用情况就这些了,文章根据IT的点点滴滴” 博客改写,如有侵犯原作者,我表示歉意这里只是学习、借鉴、总结和个人收藏。

--------------------

新增圆角按钮实现方法,不多说,直接上代码

在res目录下的drawable-mdpi建立xml文件shape.xml,代码如下:

<?xml version="1.0" encoding="UTF-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <!-- 填充的颜色 --> 
    <solid android:color="#FF9900" /> 
    <!-- 设置按钮的四个角为弧形 --> 
    <!-- android:radius 弧形的半径 --> 
    <corners android:radius="5dip" /> 
      
<!-- padding:Button里面的文字与Button边界的间隔 --> 
<padding 
   android:left="10dp" 
   android:top="10dp" 
   android:right="10dp" 
   android:bottom="10dp" 
/> 
</shape> 

在前台界面创建好按钮添加一个属性:android:background="@drawable/shape" ,完整代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testone.MainActivity" >

<Button android:id="@+id/roundButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:background="@drawable/shape" //调用上面xml中设置圆角按钮的样式
android:text="圆角按钮" />
</RelativeLayout>

显示结果如下:

ok平时学习的一点点积累都是以后慢慢成功的一小步。

posted @ 2016-05-31 20:08  学永不止步  阅读(228)  评论(0)    收藏  举报