【问题标题】:How can i change keypad button pressed color如何更改键盘按钮按下的颜色
【发布时间】:2014-03-11 03:00:59
【问题描述】:

我完全是 java 代码的菜鸟,但我想学习。 现在我设置了一个键盘,为此我输入了 3 个 orizzontal Linearlayout 并在每个中输入了 3 个按钮,所有这些都在 activity_main.xml 中,就像这样

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="286dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:inputType="numberPassword" >

    <requestFocus />
</EditText>
<LinearLayout android:orientation="horizontal" 
             android:layout_width="fill_parent" 
             android:layout_height="54.0dip" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip">

   <Button   android:textStyle="bold" 
             android:id="@+id/one"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />


    <Button  android:textStyle="bold" 
             android:id="@+id/two"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />

    <Button  android:textStyle="bold"
             android:id="@+id/three"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />
 </LinearLayout>
 <LinearLayout android:orientation="horizontal" 
             android:layout_width="fill_parent" 
             android:layout_height="54.0dip" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip">

    <Button  android:textStyle="bold"
             android:id="@+id/four"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />

    <Button  android:textStyle="bold" 
             android:id="@+id/five"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />

    <Button  android:textStyle="bold"
             android:id="@+id/six"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" 
             android:layout_width="fill_parent" 
             android:layout_height="54.0dip" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip">

    <Button  android:textStyle="bold"
             android:id="@+id/seven" 
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />

    <Button  android:textStyle="bold" 
             android:id="@+id/eight"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />

    <Button  android:textStyle="bold"
             android:id="@+id/nine"
             android:layout_width="0.0sp" 
             android:layout_height="fill_parent" 
             android:layout_marginLeft="2.0dip" 
             android:layout_marginRight="2.0dip" 
             android:layout_weight="1.0" />
</LinearLayout>

</LinearLayout>

Button的每个背景都由一个xml在drawable中定义,像这样

<?xml version="1.0" encoding="utf-8"?>
<selector
 xmlns:android="http://schemas.android.com/apk/res/android">   
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/btn_default_normal_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/btn_default_normal_disable" />
<item android:state_pressed="true" android:drawable="@drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/btn_default_selected" />
<item android:state_enabled="true" android:drawable="@drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true" android:drawable="@drawable/btn_default_normal_disable_focused" />
<item android:drawable="@drawable/btn_default_normal_disable" />

</selector>

我的 MainActivity.java 就是这个

package com.provabottone;

import android.os.Bundle;
import android.widget.Button;
import android.app.Activity;


public class MainActivity extends Activity {





private Button mOne;
private Button mTwo;
private Button mThree;
private Button mFour;
private Button mFive;
private Button mSix;
private Button mSeven;
private Button mEight;
private Button mNine;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


          mOne = (Button) findViewById(R.id.one);
          mTwo = (Button) findViewById(R.id.two);
          mThree  = (Button) findViewById(R.id.three);
          mFour = (Button) findViewById(R.id.four);
          mFive = (Button) findViewById(R.id.five);
          mSix = (Button) findViewById(R.id.six);
          mSeven = (Button) findViewById(R.id.seven);
          mEight = (Button) findViewById(R.id.eight);
          mNine = (Button) findViewById(R.id.nine);

           mOne.setText("1");
           mTwo.setText("2");
           mThree.setText("3");
           mFour.setText("4");
           mFive.setText("5");
           mSix.setText("6");
           mSeven.setText("7");
           mEight.setText("8");
           mNine.setText("9");

          mOne.setBackgroundResource(R.drawable.bottone_premuto);
          mTwo.setBackgroundResource(R.drawable.bottone_premuto);
          mThree.setBackgroundResource(R.drawable.bottone_premuto);
              mFour.setBackgroundResource(R.drawable.bottone_premuto);
          mFive.setBackgroundResource(R.drawable.bottone_premuto);
          mSix.setBackgroundResource(R.drawable.bottone_premuto);
          mSeven.setBackgroundResource(R.drawable.bottone_premuto);
          mEight.setBackgroundResource(R.drawable.bottone_premuto);
          mNine.setBackgroundResource(R.drawable.bottone_premuto);

}


}

现在我想要更改按下时由 btn_default_pressed_holo_dark 确定的按钮的颜色,我需要在 MainActivity.java 中添加什么代码? 在此先感谢,很抱歉发了很长的帖子。 嗨,尼古拉

【问题讨论】:

标签: android button android-button


【解决方案1】:

我解决了删除此代码的问题

mOne.setBackgroundResource(R.drawable.bottone_premuto);
      mTwo.setBackgroundResource(R.drawable.bottone_premuto);
      mThree.setBackgroundResource(R.drawable.bottone_premuto);
          mFour.setBackgroundResource(R.drawable.bottone_premuto);
      mFive.setBackgroundResource(R.drawable.bottone_premuto);
      mSix.setBackgroundResource(R.drawable.bottone_premuto);
      mSeven.setBackgroundResource(R.drawable.bottone_premuto);
      mEight.setBackgroundResource(R.drawable.bottone_premuto);
      mNine.setBackgroundResource(R.drawable.bottone_premuto);

并添加此代码

    mOne.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mOne.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mOne.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mTwo.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mTwo.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mTwo.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mThree.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mThree.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mThree.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mFour.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mFour.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mFour.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mFive.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mFive.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mFive.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mSix.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mSix.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mSix.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mSeven.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mSeven.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mSeven.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mEight.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mEight.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mEight.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });
    mNine.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mNine.setBackgroundColor(Color.argb(33, 33, 44, 255));
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                mNine.setBackgroundResource(R.drawable.btn_default_normal_holo_dark);
                return true;
            }
            return false;
        }
    });

但现在我想在没有 onTouchListener 的情况下以另一种方式制作它,将 android:onClick="button_pressed" 添加到我的 .xml 中的每个按钮并用这样的新方法调用它

 public void button_pressed(View v) {

   button_pressed = findViewById(R.drawable.btn_default_pressed_holo_dark);
    button_pressed.setBackgroundColor(Color.argb(33, 243, 55, 123));

}

但按钮在按下时始终显示由 btn_default_pressed_holo_dark 定义的颜色。 我做错了什么?

【讨论】:

    猜你喜欢
    • 2014-04-07
    • 2021-02-24
    • 1970-01-01
    • 2019-10-13
    • 2020-04-20
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多