0

嗨,我只想在单击转换按钮后才在edittext 2中显示结果,还有第二个按钮(清除)在我运行程序时未显示如何显示它,因为屏幕尺寸很小这里是代码

package converter.com;

import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class now extends Activity {
    /** defining variables */
    String[] currencys1,currencys2;
    String e,l,f,u,d;
    double EURO=5,dollar=6,franc=7,LE=1,UAE=8,txtvalue;
    EditText txt1,txt2;
    Button convert,clear;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        txt1 =(EditText)findViewById(R.id.txt1);

        txt2 =(EditText)findViewById(R.id.txt2);

    convert=(Button)findViewById(R.id.btn1);
      clear=(Button)findViewById(R.id.clear_btn);  


        currencys1=getResources().getStringArray(R.array.currency);
        final Spinner s1=(Spinner)findViewById(R.id.sp1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item, currencys1);
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) 
            {
                switch(arg2)
                {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                }


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

                        }
        });


        currencys2=getResources().getStringArray(R.array.currency1);
        final Spinner s2=(Spinner)findViewById(R.id.sp2);

        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item, currencys2);
        s2.setAdapter(adapter1);
 s2.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) 
            {

                txtvalue =  Double.parseDouble(txt1.getText().toString());
                switch(arg2)
                {
                case 0:
                    final String fromCurrencyCode = s1.getSelectedItem().toString();
                    final String toCurrencyCode = s2.getSelectedItem().toString();
                    if (fromCurrencyCode.equals(toCurrencyCode)) {
                        txt2.setText(txt1.getText());
                    }
                    else
                    {
                    double EURO1=txtvalue*EURO;
                    txt2.setText(String.valueOf(EURO1));
                    }                                                   
                    break;
                case 1:

                    final String fromCurrencyCode1 = s1.getSelectedItem().toString();
                    final String toCurrencyCode1 = s2.getSelectedItem().toString();
                    if (fromCurrencyCode1.equals(toCurrencyCode1)) {
                        txt2.setText(txt1.getText());
                    }
                    else
                    {

                     double LE1=txtvalue*LE;
                    txt2.setText(String.valueOf(LE1));
                    }
                    break;
                case 2:
                    final String fromCurrencyCode2 = s1.getSelectedItem().toString();
                    final String toCurrencyCode2 = s2.getSelectedItem().toString();
                    if (fromCurrencyCode2.equals(toCurrencyCode2)) {
                        txt2.setText(txt1.getText());
                    }
                    else
                    {
                         double franc1=txtvalue*franc;
                        txt2.setText(String.valueOf(franc1));
                    }
                    break;
                case 3:
                    final String fromCurrencyCode3 = s1.getSelectedItem().toString();
                    final String toCurrencyCode3 = s2.getSelectedItem().toString();
                    if (fromCurrencyCode3.equals(toCurrencyCode3)) {
                        txt2.setText(txt1.getText());
                    }
                    else
                    {
                        double UAE1=txtvalue*UAE;
                        txt2.setText(String.valueOf(UAE1));
                    }
                    break;
                case 4:
                    final String fromCurrencyCode4 = s1.getSelectedItem().toString();
                    final String toCurrencyCode4 = s2.getSelectedItem().toString();
                    if (fromCurrencyCode4.equals(toCurrencyCode4)) {
                        txt2.setText(txt1.getText());
                    }
                    else
                    {
                        double dollar1=txtvalue*dollar;
                        txt2.setText(String.valueOf(dollar1));
                    }
                    break;
                }


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

                        }
        });


        convert.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                //i want to put the code here which show result only after click that button
            }
        });
        clear.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                txt2.setText(" ");

            }
        });
    }
}

这是xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#cad1d9"
    >



    <TableLayout android:layout_width="match_parent" android:id="@+id/tableLayout1" android:layout_height="wrap_content">
        <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="from" android:textColor="#5c6471"></TextView>
    </TableLayout>
    <Spinner android:id="@+id/sp1" android:layout_height="50dip" android:layout_width="match_parent"></Spinner>
    <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Amount" android:textColor="#5c6471"></TextView>
    <EditText android:text="1" android:id="@+id/txt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:numeric="decimal"></EditText>
    <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="To" android:textColor="#5c6471"></TextView>
    <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/sp2"></Spinner>
    <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="value" android:textColor="#5c6471"></TextView>
    <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txt2" android:text="1" android:editable="false"></EditText>
    <Button android:editable="false" android:width="100px" android:gravity="center" android:id="@+id/btn1" android:text="Convert" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="@string/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:minWidth="400px" android:id="@+id/clear_btn"></Button>
</LinearLayout>
4

1 回答 1

0

txt2首次创建活动时,您可以执行以下操作来隐藏:

txt2.setVisibility(View.GONE);

然后在转换按钮的点击处理程序中:

txt2.setVisibility(View.VISIBLE);

在清除按钮的单击处理程序中,大概您想GONE再次创建它。

关于清除按钮不在屏幕上的问题:将您的包装LinearLayout在一个ScrollView(使ScrollView层次结构的顶视图具有单个LinearLayout子级)。这样,用户可以向下滚动以查看布局的所有内容。您需要将高度更改LinearLayoutwrap_content

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#cad1d9"
    >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        . . .
    </LinearLayout
</ScrollView>
于 2012-12-18T18:06:22.017 回答