使得preSS效果在安卓按钮 [英] making press effect for button in android

查看:161
本文介绍了使得preSS效果在安卓按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Android应用程序可以动态创建50按钮,它完美地工作,但问题是,当我把一些背景色为这些按钮动态按钮的preSS效应正在消失

谁能请告诉我一些解决方案,保留按钮点击结果的preSS效果
我的code如下结果给出
我的 Android平台2.3.3

 进口android.app.Activity;
进口android.os.Bundle;
进口android.view.ViewGroup.LayoutParams;
进口android.widget.Button;
进口android.widget.LinearLayout;公共类MyMain延伸活动{
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.mymain);
        createCalender();
    }
   公共无效createCalender()
   {
    的LinearLayout layoutVertical =(的LinearLayout)findViewById(R.id.liVLayout);
    LinearLayout.LayoutParams参数=新LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT,1.0F);
    param.setMargins(10,10,10,10);
    LinearLayout中的RowLayout = NULL;
    按钮[] []按钮=新的Button [10] [5];
    诠释计数= 51;
    INT标签= 1;
    的for(int i = 0;我小于10;我++)
    {
        如果(计数%5 == 1)
        {
            的RowLayout =新的LinearLayout(本);
            rowLayout.setBackgroundColor(Color.BLACK);
            rowLayout.setWeightSum(5);
            layoutVertical.addView(的RowLayout,参数);
            数=计-5;
        }
        为(中间体J = 0; J&小于5; J ++)
        {
            按钮[i] [j]的=新按钮(本);
            按钮[i] [j]的.setText(+标签);
            按钮[i] [j]的.setHeight(55);
            按钮[i] [j]的.setWidth(80);
            按钮[i] [j]的.setTextColor(Color.BLACK);
            按钮[i] [j]的.setBackgroundColor(Color.GREEN);            标签++;            rowLayout.addView(按钮[i] [j]时,参数);        }
    }
}}


解决方案

按钮[i] [j]的.setBackgroundColor(Color.GREEN);

 按钮[i] [j]的.setOnClickListener(新OnClickListener(){    @覆盖
    公共无效的onClick(视图v){        v.setBackgroundColor(Color.GRAY); //可以任意选择颜色        新的处理程序()。postDelayed(新的Runnable(){            公共无效的run(){                v.setBackgroundColor(Color.GREEN);                //按钮单击code这里
            }        },100L);到任何适合//改变这个值    }});

i've created an android application which creates 50 button dynamically,which works perfectly, but the problem is when i put some background color for these buttons dynamically the press effect of the buttons is being lost

can anyone please tell me some solution for retaining the press effect of the button click
my code is as given below
my Android Platform is 2.3.3

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

public class MyMain extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mymain);
        createCalender();
    }
   public void createCalender()
   {  
    LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT, 1.0f);


    param.setMargins(10, 10, 10, 10);


    LinearLayout rowLayout=null;
    Button[][] buttons = new Button[10][5]; 
    int count=51;
    int tab=1;
    for (int i = 0; i<10; i++) 
    {
        if(count%5==1)
        {
            rowLayout = new LinearLayout(this);
            rowLayout.setBackgroundColor(Color.BLACK);
            rowLayout.setWeightSum(5);
            layoutVertical.addView(rowLayout,param);
            count=count-5;
        }
        for(int j=0;j<5;j++)
        {
            buttons[i][j]=new Button(this);
            buttons[i][j].setText(""+tab);
            buttons[i][j].setHeight(55);
            buttons[i][j].setWidth(80);
            buttons[i][j].setTextColor(Color.BLACK);
            buttons[i][j].setBackgroundColor(Color.GREEN);

            tab++;

            rowLayout.addView(buttons[i][j],param);

        }
    }
}

}

解决方案

After buttons[i][j].setBackgroundColor(Color.GREEN);

buttons[i][j].setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        v.setBackgroundColor(Color.GRAY); // Choose whichever color

        new Handler().postDelayed(new Runnable() {

            public void run() {

                v.setBackgroundColor(Color.GREEN);

                // Button Click Code Here
            }

        }, 100L);    // Change this value to whatever is suitable

    }

});

这篇关于使得preSS效果在安卓按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆