Android 将值置于编辑文本上
我正在做一个货币转换器,但当我在editText上输入一个值并单击botton以转换时,什么都没有发生,我多次看到代码,但没有看到错误,有人能帮我吗。 我是机器人中的乞丐Android 将值置于编辑文本上,android,android-edittext,Android,Android Edittext,我正在做一个货币转换器,但当我在editText上输入一个值并单击botton以转换时,什么都没有发生,我多次看到代码,但没有看到错误,有人能帮我吗。 我是机器人中的乞丐 public class MainActivity extends Activity implements View.OnClickListener{ int money1, money2,money3,money4; int[] money = {money1,money2,money3,money4}; int fro
public class MainActivity extends Activity implements View.OnClickListener{
int money1, money2,money3,money4;
int[] money = {money1,money2,money3,money4};
int from;
TextView rate;
Button button1;
EditText ed1;
EditText ed2;
EditText ed3;
EditText ed4;
Spinner spinner1;
Spinner spinner2;
Spinner spinner3;
Spinner spinner4;
EditText[] values = {ed1,ed2,ed3,ed4};
String[] spinnerValues = { "ANGOLAN KWANZA" , "ARGENTINE PESO" , "ARMENIAN DRAM" , "ARUBAN FLORIN" , "AUSTRALIAN DOLLAR" , "BELARUSIAN RUBLE" , "BOLIVIAN BOLIVIANO" , "BRAZILIAN REAL" , "CAMBODIAN RIEL","CANADIAN DOLLAR","CHINESE YUAN RENMINBI","EURO","INDIAN RUPEE","JAPANESE YEN", "LIBIAN DINAR","SWISS FRANC","THAI BAHT","BRITISH POUND","US DOLLAR","VIETNAMESE DONG"};
String[] simbol = { "AOA", "ARS", "AMD","AWG","AUD","BYR","BOB","BRL","KHR","CAD","CNY","EUR",
"INR","JPY","CHF","THB","GBP","USD","VND","LYD"};
int arr_images[] = { R.drawable.angola, R.drawable.argentina,
R.drawable.armenia, R.drawable.aruba, R.drawable.australia,
R.drawable.bielorussia, R.drawable.bolivia, R.drawable.brasil,
R.drawable.cambodia, R.drawable.canada, R.drawable.china,
R.drawable.eur, R.drawable.india, R.drawable.japan,
R.drawable.libya, R.drawable.swiss, R.drawable.thai, R.drawable.uk,
R.drawable.us, R.drawable.vietnam };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
ed3 = (EditText) findViewById(R.id.editText3);
ed4 = (EditText) findViewById(R.id.editText4);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner4 = (Spinner) findViewById(R.id.spinner4);
Spinner mySpinner = (Spinner)
findViewById(R.id.spinner1);
mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner, spinnerValues));
Spinner mySpinner1 = (Spinner)
findViewById(R.id.spinner2);
mySpinner1.setAdapter(new MyAdapter(this, R.layout.custom_spinner, spinnerValues));
Spinner mySpinner2 = (Spinner)
findViewById(R.id.spinner3);
mySpinner2.setAdapter(new MyAdapter(this, R.layout.custom_spinner, spinnerValues));
Spinner mySpinner3 = (Spinner)
findViewById(R.id.spinner4);
mySpinner3.setAdapter(new MyAdapter(this, R.layout.custom_spinner, spinnerValues));
button1.setOnClickListener(this);
}
public class MyAdapter extends ArrayAdapter<String>
{ public MyAdapter(Context ctx, int txtViewResourceId, String[] objects)
{ super(ctx, txtViewResourceId, objects); }
@Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt)
{ return getCustomView(position, cnvtView, prnt); }
@Override
public View getView(int pos, View cnvtView, ViewGroup prnt)
{ return getCustomView(pos, cnvtView, prnt); }
public View getCustomView(int position, View convertView, ViewGroup parent)
{ LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
String[] spinnerValues = { "ANGOLAN KWANZA" , "ARGENTINE PESO" , "ARMENIAN DRAM" , "ARUBAN FLORIN" , "AUSTRALIAN DOLLAR" , "BELARUSIAN RUBLE" , "BOLIVIAN BOLIVIANO" , "BRAZILIAN REAL" , "CAMBODIAN RIEL","CANADIAN DOLLAR","CHINESE YUAN RENMINBI","EURO","INDIAN RUPEE","JAPANESE YEN", "LIBIAN DINAR","SWISS FRANC","THAI BAHT","BRITISH POUND","US DOLLAR","VIETNAMESE DONG"};
int arr_images[] = { R.drawable.angola, R.drawable.argentina,
R.drawable.armenia, R.drawable.aruba, R.drawable.australia,
R.drawable.bielorussia, R.drawable.bolivia, R.drawable.brasil,
R.drawable.cambodia, R.drawable.canada, R.drawable.china,
R.drawable.eur, R.drawable.india, R.drawable.japan,
R.drawable.libya, R.drawable.swiss, R.drawable.thai, R.drawable.uk,
R.drawable.us, R.drawable.vietnam };
TextView main_text = (TextView) mySpinner .findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
ImageView left_icon = (ImageView) mySpinner .findViewById(R.id.left_pic);
left_icon.setImageResource(arr_images[position]);
return mySpinner;
}
public View getCustomView2(int position, View convertView, ViewGroup parent)
{ LayoutInflater inflater = getLayoutInflater();
View mySpinner1 = inflater.inflate(R.layout.custom_spinner, parent, false);
String[] spinnerValues = { "ANGOLAN KWANZA" , "ARGENTINE PESO" , "ARMENIAN DRAM" , "ARUBAN FLORIN" , "AUSTRALIAN DOLLAR" , "BELARUSIAN RUBLE" , "BOLIVIAN BOLIVIANO" , "BRAZILIAN REAL" , "CAMBODIAN RIEL","CANADIAN DOLLAR","CHINESE YUAN RENMINBI","EURO","INDIAN RUPEE","JAPANESE YEN", "LIBIAN DINAR","SWISS FRANC","THAI BAHT","BRITISH POUND","US DOLLAR","VIETNAMESE DONG"};
int arr_images[] = { R.drawable.angola, R.drawable.argentina,
R.drawable.armenia, R.drawable.aruba, R.drawable.australia,
R.drawable.bielorussia, R.drawable.bolivia, R.drawable.brasil,
R.drawable.cambodia, R.drawable.canada, R.drawable.china,
R.drawable.eur, R.drawable.india, R.drawable.japan,
R.drawable.libya, R.drawable.swiss, R.drawable.thai, R.drawable.uk,
R.drawable.us, R.drawable.vietnam };
TextView main_text = (TextView) mySpinner1 .findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
ImageView left_icon = (ImageView) mySpinner1 .findViewById(R.id.left_pic);
left_icon.setImageResource(arr_images[position]);
return mySpinner1;}
public View getCustomView3(int position, View convertView, ViewGroup parent)
{ LayoutInflater inflater = getLayoutInflater();
View mySpinner2 = inflater.inflate(R.layout.custom_spinner, parent, false);
String[] spinnerValues = { "ANGOLAN KWANZA" , "ARGENTINE PESO" , "ARMENIAN DRAM" , "ARUBAN FLORIN" , "AUSTRALIAN DOLLAR" , "BELARUSIAN RUBLE" , "BOLIVIAN BOLIVIANO" , "BRAZILIAN REAL" , "CAMBODIAN RIEL","CANADIAN DOLLAR","CHINESE YUAN RENMINBI","EURO","INDIAN RUPEE","JAPANESE YEN", "LIBIAN DINAR","SWISS FRANC","THAI BAHT","BRITISH POUND","US DOLLAR","VIETNAMESE DONG"};
int arr_images[] = { R.drawable.angola, R.drawable.argentina,
R.drawable.armenia, R.drawable.aruba, R.drawable.australia,
R.drawable.bielorussia, R.drawable.bolivia, R.drawable.brasil,
R.drawable.cambodia, R.drawable.canada, R.drawable.china,
R.drawable.eur, R.drawable.india, R.drawable.japan,
R.drawable.libya, R.drawable.swiss, R.drawable.thai, R.drawable.uk,
R.drawable.us, R.drawable.vietnam };
TextView main_text = (TextView) mySpinner2 .findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
ImageView left_icon = (ImageView) mySpinner2 .findViewById(R.id.left_pic);
left_icon.setImageResource(arr_images[position]);
return mySpinner2;}
public View getCustomView4(int position, View convertView, ViewGroup parent)
{ LayoutInflater inflater = getLayoutInflater();
View mySpinner3 = inflater.inflate(R.layout.custom_spinner, parent, false);
String[] spinnerValues = { "ANGOLAN KWANZA" , "ARGENTINE PESO" , "ARMENIAN DRAM" , "ARUBAN FLORIN" , "AUSTRALIAN DOLLAR" , "BELARUSIAN RUBLE" , "BOLIVIAN BOLIVIANO" , "BRAZILIAN REAL" , "CAMBODIAN RIEL","CANADIAN DOLLAR","CHINESE YUAN RENMINBI","EURO","INDIAN RUPEE","JAPANESE YEN", "LIBIAN DINAR","SWISS FRANC","THAI BAHT","BRITISH POUND","US DOLLAR","VIETNAMESE DONG"};
int arr_images[] = { R.drawable.angola, R.drawable.argentina,
R.drawable.armenia, R.drawable.aruba, R.drawable.australia,
R.drawable.bielorussia, R.drawable.bolivia, R.drawable.brasil,
R.drawable.cambodia, R.drawable.canada, R.drawable.china,
R.drawable.eur, R.drawable.india, R.drawable.japan,
R.drawable.libya, R.drawable.swiss, R.drawable.thai, R.drawable.uk,
R.drawable.us, R.drawable.vietnam };
TextView main_text = (TextView) mySpinner3 .findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
ImageView left_icon = (ImageView) mySpinner3 .findViewById(R.id.left_pic);
left_icon.setImageResource(arr_images[position]);
return mySpinner3;}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
void getRate(int from, int to) {
String currency;
String currency2;
currency2 = simbol[from];
int i;
for (i=0;i<=3;i=i+1){
to=money[i];
currency=simbol[to];
GetExchangeRate httpRate;
httpRate = new GetExchangeRate(currency, currency2);
Thread thr = new Thread(httpRate);
thr.start();
}}}
private void writeRate(final String text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
int i;
for(i=0;i<=3;i=i+1){
values[i].setText(text);
}
}
});
}
private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuffer response = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
response.append(line);
}
}
catch (IOException e) {
return e.getMessage();
}
finally {
if (reader != null) {
try {
reader.close();
}
catch (IOException e) {
return e.getMessage();
}
}
}
return response.toString();
}
private String transform(String rate) {
String readableRate = null;
JSONObject json;
try {
json = new JSONObject(rate);
readableRate = " " + json.getDouble("rate");
}
catch (JSONException e) {
readableRate = e.toString();
}
return readableRate;
}
//**************************************************************************
//Internal class to call HTTP operation in a separate thread
private class GetExchangeRate implements Runnable {
String currency;
String currency2;
GetExchangeRate(String toCurrency, String fromCurrency) {
currency = toCurrency;
currency2= fromCurrency;
}
@Override
public void run() {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://rate-exchange.appspot.com/currency?from=" + currency2 + "&to=" + currency);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setUseCaches(false);
int responseCode = urlConnection.getResponseCode();
if(responseCode == HttpStatus.SC_OK) {
String response = readStream(urlConnection.getInputStream());
response = transform(response);
writeRate(response);
}
else
writeRate("Code: " + responseCode);
}
catch (Exception e) {
writeRate(e.toString());
}
finally {
if(urlConnection != null)
urlConnection.disconnect();
}
}
}
@Override
public void onClick(View v) {
money1 = Integer.parseInt(String.valueOf(spinner1.getSelectedItemPosition()));
money2 = Integer.parseInt(String.valueOf(spinner2.getSelectedItemPosition()));
money3 = Integer.parseInt(String.valueOf(spinner3.getSelectedItemPosition()));
money4 = Integer.parseInt(String.valueOf(spinner4.getSelectedItemPosition()));
int[] money = {money1,money2,money3,money4};
int i;
for(i=0; i<=3;i=i+1)
if(values[i].getText().toString().trim().length() != 0){
from=money[i];
return;}
}}
public类MainActivity扩展活动实现View.OnClickListener{
int money1、money2、money3、money4;
int[]money={money1,money2,money3,money4};
int from;
文本浏览率;
按钮1;
编辑文本ed1;
编辑文本ed2;
编辑文本ed3;
编辑文本ed4;
喷丝器喷丝器1;
喷丝器喷丝器2;
喷丝器喷丝器3;
喷丝器喷丝器4;
EditText[]值={ed1,ed2,ed3,ed4};
字符串[]spinnerValues={“安哥拉宽扎”、“阿根廷比索”、“亚美尼亚德拉姆”、“阿鲁班弗洛林”、“澳大利亚元”、“白俄罗斯卢布”、“玻利维亚诺”、“巴西雷亚尔”、“柬埔寨瑞尔”、“加拿大元”、“人民币”、“欧元”、“印度卢比”、“日元”、“利比亚第纳尔”、“瑞士法郎”、“泰铢”,“英镑”、“美元”、“越南盾”;
字符串[]simbol={“AOA”、“ARS”、“AMD”、“AWG”、“AUD”、“BYR”、“BOB”、“BRL”、“KHR”、“CAD”、“CNY”、“EUR”,
“印度卢比”、“日元”、“瑞士法郎”、“泰铢”、“英镑”、“美元”、“越南盾”、“荷兰盾”};
int arr_images[]={R.dravable.安哥拉,R.dravable.阿根廷,
R.drawable.亚美尼亚,R.drawable.阿鲁巴,R.drawable.澳大利亚,
R.drawable.bielorussia,R.drawable.玻利维亚,R.drawable.brasil,
柬埔寨,加拿大,中国,
可支取欧元,可支取印度,可支取日本,
R.drawable.利比亚,R.drawable.swiss,R.drawable.thai,R.drawable.uk,
R.dravable.us,R.dravable.越南};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(按钮)findViewById(R.id.button1);
ed1=(EditText)findViewById(R.id.editText1);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
ed4=(EditText)findViewById(R.id.editText4);
喷丝头1=(喷丝头)findViewById(R.id.spinner1);
喷丝头2=(喷丝头)findViewById(R.id.spinner2);
喷丝头3=(喷丝头)findViewById(R.id.spinner3);
喷丝头4=(喷丝头)findViewById(R.id.spinner4);
微调器mySpinner=(微调器)
FindViewbyd(R.id.spinner1);
setAdapter(新的MyAdapter(this,R.layout.custom_微调器,spinnerValues));
微调器mySpinner1=(微调器)
findViewById(R.id.spinner2);
setAdapter(新的MyAdapter(this,R.layout.custom_微调器,spinnerValues));
微调器mySpinner2=(微调器)
findviewbyd(R.id.spinner3);
setAdapter(新的MyAdapter(this,R.layout.custom_微调器,spinnerValues));
微调器mySpinner3=(微调器)
findviewbyd(R.id.spinner4);
setAdapter(新的MyAdapter(this,R.layout.custom_微调器,spinnerValues));
button1.setOnClickListener(此);
}
公共类MyAdapter扩展了ArrayAdapter
{公共MyAdapter(上下文ctx,int-txtViewResourceId,字符串[]对象)
{super(ctx,txtViewResourceId,objects);}
@凌驾
公共视图getDropDownView(内部位置、视图cnvtView、视图组prnt)
{返回getCustomView(位置,cnvtView,prnt);}
@凌驾
公共视图getView(内部位置、视图cnvtView、视图组prnt)
{返回getCustomView(pos、cnvtView、prnt);}
公共视图getCustomView(int位置、视图转换视图、视图组父视图)
{LayoutInflater充气机=getLayoutInflater();
查看mySpinner=充气器。充气(R.layout.custom_微调器,父项,false);
字符串[]spinnerValues={“安哥拉宽扎”、“阿根廷比索”、“亚美尼亚德拉姆”、“阿鲁班弗洛林”、“澳大利亚元”、“白俄罗斯卢布”、“玻利维亚诺”、“巴西雷亚尔”、“柬埔寨瑞尔”、“加拿大元”、“中国元人民币”、“欧元”、“印度卢比”、“日元”、“利比亚第纳尔”、“瑞士法郎”、“泰铢”、“英镑”美元,"越南盾";;
int arr_images[]={R.dravable.安哥拉,R.dravable.阿根廷,
R.drawable.亚美尼亚,R.drawable.阿鲁巴,R.drawable.澳大利亚,
R.drawable.bielorussia,R.drawable.玻利维亚,R.drawable.brasil,
柬埔寨,加拿大,中国,
可支取欧元,可支取印度,可支取日本,
R.drawable.利比亚,R.drawable.swiss,R.drawable.thai,R.drawable.uk,
R.dravable.us,R.dravable.越南};
TextView main\u text=(TextView)mySpinner.findViewById(R.id.text\u main\u seed);
main_text.setText(spinnerValues[position]);
ImageView left_icon=(ImageView)mySpinner.findViewById(R.id.left_pic);
左_图标。设置图像资源(arr_图像[位置]);
返回mySpinner;
}
公共视图getCustomView2(内部位置、视图转换视图、视图组父视图)
{LayoutInflater充气机=getLayoutInflater();
View mySpinner1=充气器。充气(R.layout.custom_微调器,父项,false);
字符串[]spinnerValues={“安哥拉宽扎”、“阿根廷比索”、“亚美尼亚德拉姆”、“阿鲁班弗洛林”、“澳大利亚元”、“白俄罗斯卢布”、“玻利维亚诺”、“巴西雷亚尔”、“柬埔寨瑞尔”、“加拿大元”、“中国元人民币”、“欧元”、“印度卢比”、“日元”、“利比亚第纳尔”、“瑞士法郎”、“泰铢”、“英镑”美元,"越南盾";;
int arr_images[]={R.dravable.安哥拉,R.dravable.阿根廷,
R.drawable.亚美尼亚,R.drawable.阿鲁巴,R.drawable.澳大利亚,
R.drawable.bielorussia,R.drawable.玻利维亚,R.drawable.brasil,
柬埔寨,加拿大,中国,
可支取欧元,可支取印度,可支取日本,
R.drawable.利比亚,R.drawable.swiss,R.drawable.thai,R.drawable.uk,
R.dravable.us,R.dravable.越南};
TextView main\u text=(TextView)mySpinner1.findViewById(R.id.text\u main\u seed);
main_text.setText(spinnerValues[position]);
ImageView left_icon=(ImageView)mySpinner1.findViewById(R.id.left_pic);
左_图标。设置图像资源(arr_图像[位置]);
返回mySpinner1;}
公共视图getCustomView3(内部位置,视图转换)
button1.setOnClickListener(this);
// here call getRate with the appropriate arguments
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
ed3 = (EditText) findViewById(R.id.editText3);
ed4 = (EditText) findViewById(R.id.editText4);
values[0]=ed1;
values[1]=ed2;
values[2]=ed3;
values[3]=ed4;
void getRate(int from, int to) {
String currency;
String currency2;
currency2 = simbol[from];
int i;
for (i=0;i<=3;i=i+1){
to=money[i];
currency=simbol[to];
GetExchangeRate httpRate;
httpRate = new GetExchangeRate(currency, currency2);
Thread thr = new Thread(httpRate);
thr.start();
}}}//behind last of this brackets
}
void getRate(int from, int to) {
String currency;
String currency2;
currency2 = simbol[from];
int i;
for (i=0;i<=3;i=i+1){
to=money[i];
currency=simbol[to];
GetExchangeRate httpRate;
httpRate = new GetExchangeRate(currency, currency2);
Thread thr = new Thread(httpRate);
thr.start();
}}
@Override
public void onClick(View v) {
money1 = Integer.parseInt(String.valueOf(spinner1.getSelectedItemPosition()));
money2 = Integer.parseInt(String.valueOf(spinner2.getSelectedItemPosition()));
money3 = Integer.parseInt(String.valueOf(spinner3.getSelectedItemPosition()));
money4 = Integer.parseInt(String.valueOf(spinner4.getSelectedItemPosition()));
int[] money = {money1,money2,money3,money4};
int i;
for(i=0; i<=3;i=i+1)
if(values[i].getText().toString().trim().length() != 0){
from=money[i];
getRate( from,0) ;
return;
}
}