我想按下按钮背景及其文字颜色。
请检查我的以下项目并告诉我你的建议......
这是我的 MainActivity.java
package com.example.dynamicbutton;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout myLayout = (LinearLayout) findViewById(R.id.layout);
myLayout.setPadding(0, 10, 0, 30);
Button[] btn = new Button[5];
for(int i=0; i<5; i++)
{
btn[i] = new Button(this);
btn[i].setId(i);
btn[i].setText("Dynamic Button" + i);
btn[i].setPadding(5, 5, 5, 5);
btn[i].setTextSize(15);
if (i % 2 == 0)
{
btn[i].setBackgroundResource(R.drawable.bluebutton_background);
btn[i].setTextColor(getResources().getColor(R.drawable.blue_text_color));
}
else{
btn[i].setBackgroundResource(R.drawable.graybutton_background);
btn[i].setTextColor(getResources().getColor(R.drawable.gray_text_color));
}
myLayout.addView(btn[i]);
}
}
@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;
}
}
和 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="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
和 Drawables:
bluebutton_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_focused="true"
android:drawable="@drawable/orangebutton"
/>
<item
android:state_pressed="true"
android:drawable="@drawable/orangebutton"
/>
<item android:drawable="@drawable/bluebutton"/>
</selector>
graybutton_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@drawable/orangebutton"/>
<item android:state_pressed="true" android:drawable="@drawable/orangebutton"/>
<item android:drawable="@drawable/graybutton"/>
</selector>
gray_text_color.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:color="#f4f4f4"/>
<item android:state_pressed="true" android:color="#242425"/>
<item android:color="#f4f4f4"/>
</selector>
blue_text_color.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:color="#242425"/>
<item android:state_pressed="true" android:color="#242425"/>
<item android:color="#242425"/>
</selector>
最诚挚的问候
答案 0 :(得分:0)
您可以使用onClickListener()方法。
尝试以下步骤:
将您的按钮数组声明为
final Button [] btn = new Button [5];
在for循环中声明一个最终的int j
final int j = i;
将以下代码放在for循环中的if else部分之后
btn [i] .setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(btn[j].getTextColors().getDefaultColor() == Color.parseColor("#242425")){
btn[j].setTextColor(getResources().getColor(R.drawable.gray_text_color));
btn[i].setBackgroundResource(R.drawable.graybutton_background);
} else {
btn[j].setTextColor(getResources().getColor(R.drawable.blue_text_color));
btn[i].setBackgroundResource(R.drawable.bluebutton_background);
}
}
});
最终代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout myLayout = (LinearLayout) findViewById(R.id.layout);
myLayout.setPadding(0, 10, 0, 30);
final Button[] btn = new Button[5]; // 1
for(int i=0; i<5; i++)
{
final int j = i; // 2
btn[i] = new Button(this);
btn[i].setId(i);
btn[i].setText("Dynamic Button" + i);
btn[i].setPadding(5, 5, 5, 5);
btn[i].setTextSize(15);
if (i % 2 == 0)
{
btn[i].setBackgroundResource(R.drawable.bluebutton_background);
btn[i].setTextColor(getResources().getColor(R.drawable.blue_text_color));
}
else{
btn[i].setBackgroundResource(R.drawable.graybutton_background);
btn[i].setTextColor(getResources().getColor(R.drawable.gray_text_color));
}
myLayout.addView(btn[i]);
}
btn[i].setOnClickListener(new OnClickListener() { // 3
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(btn[j].getTextColors().getDefaultColor() == Color.parseColor("#242425")){
btn[j].setTextColor(getResources().getColor(R.drawable.gray_text_color));
btn[i].setBackgroundResource(R.drawable.graybutton_background);
} else {
btn[j].setTextColor(getResources().getColor(R.drawable.blue_text_color));
btn[i].setBackgroundResource(R.drawable.bluebutton_background);
}
}
});
}
答案 1 :(得分:0)
我在图片中解释了我的意思。
我将需要我的按钮 background_image 和 textColor 一起更改。
我使用选择器标记用于 background_image 。
此外,当我按下按钮时,我使用选择器标记更改 textColor 。
当我按下我的按钮时, background_image 会发生变化,但 textColor 只会提供默认值
.xml 文件的值( blue_text_color.xml 或 gray_text_color.xml )