我是一个完全没有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确定的
<?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中添加什么代码? 提前谢谢,抱歉很长的帖子。 嗨Nicola
答案 0 :(得分:0)
我解决了删除此代码的问题
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定义的颜色。 我做错了什么?