我想写我的第一个应用程序 - 一个简单的应用程序,其中用户输入十六进制颜色代码(EditText),命中输入(按钮),并改变ImageView的背景颜色到用户输入的十六进制代码。我如何看待它,我将不得不从edittext中获取文本,将gettext写入文件,然后编辑文件以在十六进制代码之前附加0xAA,以便可以在ImageView.setBackgroundColor(0xAA“HEXHEX”)中输入它。请让我知道我该如何做到这一点,或者如果这是做到这一点的正确方法。Android:更改ImageView背景与EditText输入
这里是我的Java至今(上按一下按钮,背景颜色变为白色,明确其恢复为黑色)
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
public class ChkhexActivity extends Activity {
private EditText hex;
private Button chk;
private Button clear;
private ImageView view;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
hex = (EditText)findViewById(R.id.hex);
chk = (Button)findViewById(R.id.chk);
clear = (Button)findViewById(R.id.clear);
view = (ImageView)findViewById(R.id.view);
chk.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ Chk(); }});
clear.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ Clear(); }});
}
private void Chk()
{
view.setBackgroundColor(0xFFffffff);
}
private void Clear()
{
hex.setText("");
view.setBackgroundColor(0xFF000000);
}
}
http://stackoverflow.com/questions/1097742/string-to-hex-value并使用'+'来连接字符串。 – 2012-02-17 05:58:55