我想寫我的第一個應用程序 - 一個簡單的應用程序,其中用戶輸入十六進制顏色代碼(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