Android-运行示例代码[dimens.xml]

时间:2018-04-12 12:17:34

标签: java android xml

我是Android Studio新手,在运行示例代码方面存在一些问题: activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.abdul.moqueet.currency.MainActivity">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="5"
    android:id="@+id/et"
    android:layout_marginTop="33dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />


<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spin"
    android:layout_alignTop="@+id/et"
    android:layout_toRightOf="@+id/et"
    android:layout_toEndOf="@+id/et"
    android:layout_marginLeft="47dp"
    android:layout_marginStart="47dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/convert"
    android:id="@+id/btn"
    android:layout_marginTop="33dp"
    android:layout_below="@+id/spin"
    android:layout_toRightOf="@+id/et"
    android:layout_toEndOf="@+id/et" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/usdtxt"
    android:id="@+id/TextView1"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/btn"
    android:layout_alignLeft="@+id/spin"
    android:layout_alignStart="@+id/spin"
    android:layout_marginLeft="80dp"
    android:layout_marginStart="80dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/eurtxt"
    android:id="@+id/TextView2"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/TextView1"
    android:layout_alignLeft="@+id/spin"
    android:layout_alignStart="@+id/spin"
    android:layout_marginLeft="80dp"
    android:layout_marginStart="80dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/inrtxt"
    android:id="@+id/TextView3"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/TextView2"
    android:layout_alignLeft="@+id/spin"
    android:layout_alignStart="@+id/spin"
    android:layout_marginLeft="80dp"
    android:layout_marginStart="80dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/zero"
    android:id="@+id/usd"
    android:layout_alignTop="@+id/TextView1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="17dp"
    android:layout_marginStart="17dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/zero"
    android:id="@+id/euro"
    android:layout_above="@+id/TextView3"
    android:layout_alignLeft="@+id/usd"
    android:layout_alignStart="@+id/usd" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/zero"
    android:id="@+id/inr"
    android:layout_alignTop="@+id/TextView3"
    android:layout_alignLeft="@+id/euro"
    android:layout_alignStart="@+id/euro" />
    </RelativeLayout>

MainActivity.java

package com.example.adriangranosik.coinconverter;

    import android.os.AsyncTask;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.TextView;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    public class MainActivity extends AppCompatActivity {

        private EditText et;
        private TextView usd, euro, inr;
        private Button btn;
        private Spinner spin;
        private int index = 0;
        private double inputvalue;
        private String result[] = new String[10];

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            et = (EditText) findViewById(R.id.et);
            usd = (TextView) findViewById(R.id.usd);
            euro = (TextView) findViewById(R.id.euro);
            inr = (TextView) findViewById(R.id.inr);
            btn = (Button) findViewById(R.id.btn);
            spin = (Spinner) findViewById(R.id.spin);

            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
            spin.setAdapter(adapter);

            spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    index = position;
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    usd.setText("wait...");
                    euro.setText("wait...");
                    inr.setText("wait...");

                    if (et.getText().toString().trim().length() > 0 && !et.getText().toString().trim().equals(".")) {
                        String textValue = et.getText().toString();
                        inputvalue = Double.parseDouble(textValue);

                        new calculate().execute();
                    }
                }
            });
        }

        public class calculate extends AsyncTask<String, String, String[]> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected String[] doInBackground(String... params) {
                if (index == 0) {
                    String uRl;
                    try {
                        uRl = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDEUR,USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys");
                        JSONObject USDtojObj;
                        USDtojObj = new JSONObject(uRl);

                        JSONArray rateArray = USDtojObj.getJSONObject("query").getJSONObject("results").getJSONArray("rate");
                        result[0] = rateArray.getJSONObject(0).getString("Rate");
                        result[1] = rateArray.getJSONObject(1).getString("Rate");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else if (index == 1) {
                    String uRl;
                    try {
                        uRl = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22EURUSD,EURINR%22)&format=json&env=store://datatables.org/alltableswithkeys");
                        JSONObject EurotojObj;
                        EurotojObj = new JSONObject(uRl);

                        JSONArray rateArray = EurotojObj.getJSONObject("query").getJSONObject("results").getJSONArray("rate");
                        result[0] = rateArray.getJSONObject(0).getString("Rate");
                        result[1] = rateArray.getJSONObject(1).getString("Rate");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else if (index == 2) {
                    String uRl;
                    try {
                        uRl = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22INRUSD,INREUR%22)&format=json&env=store://datatables.org/alltableswithkeys");
                        JSONObject INRtojObj;
                        INRtojObj = new JSONObject(uRl);

                        JSONArray rateArray = INRtojObj.getJSONObject("query").getJSONObject("results").getJSONArray("rate");
                        result[0] = rateArray.getJSONObject(0).getString("Rate");
                        result[1] = rateArray.getJSONObject(1).getString("Rate");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                return result;
            }

            @Override
            protected void onPostExecute(String[] strings) {
                if(index == 0){
                    double usdtoeuroval, usdtoinrval, usdtoeuroinp, usdtoinrinp, usdtousdinp;
                    usdtousdinp = inputvalue * 1;
                    usd.setText(""+usdtousdinp);

                    usdtoeuroval = Double.parseDouble(result[0]);
                    usdtoeuroinp = inputvalue * usdtoeuroval;
                    euro.setText(""+usdtoeuroinp);

                    usdtoinrval = Double.parseDouble(result[1]);
                    usdtoinrinp = inputvalue * usdtoinrval;
                    inr.setText(""+usdtoinrinp);
                }else if(index == 1){
                    double eurotousdval, eurotoinrval, eurotousdinp, eurotoinrinp, eurotoeuroinp;
                    eurotoeuroinp = inputvalue * 1;
                    euro.setText(""+eurotoeuroinp);

                    eurotousdval = Double.parseDouble(result[0]);
                    eurotousdinp = inputvalue * eurotousdval;
                    usd.setText(""+eurotousdinp);

                    eurotoinrval = Double.parseDouble(result[1]);
                    eurotoinrinp = inputvalue * eurotoinrval;
                    inr.setText(""+eurotoinrinp);
                }else if(index == 2){
                    double inrtousdval, inrtoeuroval, inrtousdinp, inrtoeuroinp, inrtoinrinp;

                    inrtoinrinp = inputvalue * 1;
                    inr.setText(""+inrtoinrinp);

                    inrtousdval = Double.parseDouble(result[0]);
                    inrtousdinp = inputvalue * inrtousdval;
                    usd.setText(""+inrtousdinp);

                    inrtoeuroval = Double.parseDouble(result[1]);
                    inrtoeuroinp = inputvalue * inrtoeuroval;
                    euro.setText(""+inrtoeuroinp);
                }
            }

            public String getJson(String url) throws ClientProtocolException, IOException {

                StringBuilder build = new StringBuilder();
                HttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                HttpResponse response = client.execute(httpGet);
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String con;
                while ((con = reader.readLine()) != null) {
                    build.append(con);
                }
                return build.toString();
            }
        }
    }

我遇到错误:

  

布局/ activity_main.xml中
      错误:找不到资源维度/ activity_vertical_margin(又名com.example.adriangranosik.coinconverter:dimen / activity_vertical_margin)。       错误:找不到资源维度/ activity_horizo​​ntal_margin(又名com.example.adriangranosik.coinconverter:dimen / activity_horizo​​ntal_margin)。       错误:找不到资源字符串/转换(也就是com.example.adriangranosik.coinconverter:string / convert)。       错误:找不到资源字符串/ usdtxt(又名com.example.adriangranosik.coinconverter:string / usdtxt)。
      错误:找不到资源字符串/ eurtxt(又名com.example.adriangranosik.coinconverter:string / eurtxt)。
      错误:找不到资源字符串/ inrtxt(又名com.example.adriangranosik.coinconverter:string / inrtxt)。
      错误:找不到资源字符串/零(也就是com.example.adriangranosik.coinconverter:string / zero)。
      错误:找不到资源字符串/零(也就是com.example.adriangranosik.coinconverter:string / zero)。
      错误:找不到资源字符串/零(也就是com.example.adriangranosik.coinconverter:string / zero)。
      空
      链接文件资源失败。

我只是想运行一个程序来分析源代码。我知道有dimens.xml文件丢失,但我不知道该放什么。 我是Android开发人员的新手,所以我找到了示例代码,只是为了学习如何创建这样的应用程序。但是当我无法运行它时很难:D

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

您可以直接替换

android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"

与smth一样

android:paddingBottom="8dp" android:paddingLeft="8dp" android:paddingRight="8dp" android:paddingTop="8dp"

(或者如果所有4个填充相等,则可以只设置android:padding="8dp"

答案 1 :(得分:0)

在res - &gt;值将此代码放在dimens.xml

<dimen name="activity_vertical_margin">3dp</dimen>

根据您的用户界面调整dp。

答案 2 :(得分:0)

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

将以上代码替换为RelativeLayout中的以下行:

android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"