0

大家好,我有一个小问题。我在滚动视图中有 7 个编辑文本,当我在第一个编辑文本中放入一些数据并向下滚动以按下按钮(进行某些计算)时,我丢失了数据,焦点转到其他编辑文本。谁能帮忙我出去。非常感谢

4

2 回答 2

0
public class LengthMainActivity extends Activity implements OnFocusChangeListener {
   private Button  convert;
   private Button back;
   private EditText edtext1,edtext2,edtext3,edtext4,edtext5,edtext6,edtext7;
   private Button clear;
   private float numb;
   private String labelon; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.length_main);


        addListenerOnButton();


         edtext1 =  (EditText)this.findViewById(R.id.editText1);

            edtext2 =  (EditText)this.findViewById(R.id.editText2);
            edtext3 =  (EditText)this.findViewById(R.id.editText3);
            edtext4 =  (EditText)this.findViewById(R.id.editText4);
            edtext5 =  (EditText)this.findViewById(R.id.editText5);
            edtext6 =  (EditText)this.findViewById(R.id.editText6);
            edtext7 =  (EditText)this.findViewById(R.id.editText7);

            edtext1.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View arg0, boolean hasFocus) {
                // TODO Auto-generated method stub

                if(edtext1.hasFocus())
                    {getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    edtext7.clearFocus();edtext2.clearFocus();edtext3.clearFocus();edtext4.clearFocus();edtext5.clearFocus();edtext6.clearFocus();          
            edtext2.setText("");edtext3.setText("");edtext4.setText("");edtext5.setText("");edtext6.setText("");edtext7.setText("");
                    }

            }
        }) ;

           edtext2.setOnFocusChangeListener(new View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View arg0, boolean hasFocus) {
                    // TODO Auto-generated method stub
                    if(edtext2.hasFocus())
                    {getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                edtext1.clearFocus();edtext7.clearFocus();edtext3.clearFocus();edtext4.clearFocus();edtext5.clearFocus();edtext6.clearFocus();  
                edtext1.setText("");edtext3.setText("");edtext4.setText("");edtext5.setText("");edtext6.setText("");edtext7.setText("");    

                }
                }
            }) ;

           edtext3.setOnFocusChangeListener(new View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View arg0, boolean hasFocus) {
                    // TODO Auto-generated method stub
                    if(edtext3.hasFocus())
                    {getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    edtext1.clearFocus();edtext2.clearFocus();edtext7.clearFocus();edtext4.clearFocus();edtext5.clearFocus();edtext6.clearFocus();
                edtext1.setText("");edtext2.setText("");edtext4.setText("");edtext5.setText("");edtext6.setText("");edtext7.setText("");    

                }
                }
            }) ;

           edtext4.setOnFocusChangeListener(new View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View arg0, boolean hasFocus) {
                    // TODO Auto-generated method stub
                    if(edtext4.hasFocus())
                    {getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    edtext1.clearFocus();edtext2.clearFocus();edtext3.clearFocus();edtext7.clearFocus();edtext5.clearFocus();edtext6.clearFocus();      
                edtext1.setText("");edtext2.setText("");edtext3.setText("");edtext5.setText("");edtext6.setText("");edtext7.setText("");    

                }
                }
            }) ;

           edtext5.setOnFocusChangeListener(new View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View arg0, boolean hasFocus) {
                    // TODO Auto-generated method stub
                    if(edtext5.hasFocus())
                    {getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    edtext1.clearFocus();edtext2.clearFocus();edtext3.clearFocus();edtext4.clearFocus();edtext7.clearFocus();edtext6.clearFocus();
                edtext1.setText("");edtext2.setText("");edtext3.setText("");edtext4.setText("");edtext6.setText("");edtext7.setText("");    

                }
                }
            }) ;

           edtext6.setOnFocusChangeListener(new View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View arg0, boolean hasFocus) {
                    // TODO Auto-generated method stub
                    if(edtext6.hasFocus())
                    {
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);   
                    edtext1.clearFocus();edtext2.clearFocus();edtext3.clearFocus();edtext4.clearFocus();edtext5.clearFocus();edtext7.clearFocus();
                edtext1.setText("");edtext2.setText("");edtext3.setText("");edtext4.setText("");edtext5.setText("");edtext7.setText("");    
                    }
                }
            }) ;

           edtext7.setOnFocusChangeListener(new View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View arg0, boolean hasFocus) {
                    // TODO Auto-generated method stub
                    if(edtext7.hasFocus())
                    {
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    edtext1.clearFocus();edtext2.clearFocus();edtext3.clearFocus();edtext4.clearFocus();edtext5.clearFocus();edtext6.clearFocus();   
                    edtext1.setText("");edtext2.setText("");edtext3.setText("");edtext4.setText("");edtext5.setText("");edtext6.setText("");    
                    }
                }
            }) ;


        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.length_main, menu);
        return true;
    }
    private void addListenerOnButton() {
        convert = (Button)this.findViewById(R.id.button1);
         convert.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v)
            {
               try
               {

            if(edtext1.hasFocus())
                {
                labelon = edtext1.getText().toString();
                     numb =  Float.parseFloat(labelon);
                    double  result = numb*1000;
                   String res = String.valueOf(result);
                    Toast.makeText(LengthMainActivity.this, res, Toast.LENGTH_SHORT).show();

                }

                else if (edtext2.hasFocus())
                {   
                    labelon = edtext2.getText().toString();
                   numb =  Float.parseFloat(labelon);

                    double  result = numb*1000;
                       String res = String.valueOf(result);
                    Toast.makeText(LengthMainActivity.this, res, Toast.LENGTH_SHORT).show();

                }
                else if(edtext3.hasFocus())
                {   
                    labelon = edtext3.getText().toString();
                     numb =  Float.parseFloat(labelon);

                    double  result = numb*.621371;
                   String res = String.valueOf(result);
                Toast.makeText(LengthMainActivity.this, res, Toast.LENGTH_SHORT).show();

                }
                else if(edtext4.hasFocus())
                {       
                    labelon = edtext4.getText().toString();
                    numb =  Float.parseFloat(labelon);

                    double  result = numb*0.6213710;
                   String res = String.valueOf(result);
                Toast.makeText(LengthMainActivity.this, res, Toast.LENGTH_SHORT).show();

                }
                else if(edtext5.hasFocus())
                {       
                    labelon = edtext5.getText().toString();
                    numb =  Float.parseFloat(labelon);

                    double  result = numb*30.48;
                   String res = String.valueOf(result);
                Toast.makeText(LengthMainActivity.this, res, Toast.LENGTH_SHORT).show();

                }

                else if(edtext6.hasFocus())
                {       
                    labelon = edtext6.getText().toString();
                    numb =  Float.parseFloat(labelon);

                    double  result = numb*12;
                   String res = String.valueOf(result);
                Toast.makeText(LengthMainActivity.this, res, Toast.LENGTH_SHORT).show();

                }
                else if (edtext7.hasFocus())
                {   
                    labelon = edtext7.getText().toString();
                   numb =  Float.parseFloat(labelon);

                    double  result = numb*(.3937);
                       String res = String.valueOf(result);
                    Toast.makeText(LengthMainActivity.this, res, Toast.LENGTH_SHORT).show();

                }


                else //if((edtext1.getText().toString().length()==0) &&(edtext2.getText().toString().length()==0)&&(edtext3.getText().toString().length()==0)&&(edtext4.getText().toString().length()==0))
                {

                    Toast.makeText(LengthMainActivity.this, "Please provide vlaues", Toast.LENGTH_SHORT).show();
                }
            }
               catch(NumberFormatException ne)
               {
                   System.err.print("exception is::::" + ne);
               }
            }

        });

        back = (Button)this.findViewById(R.id.button2);
        back.setOnClickListener(new OnClickListener()
        {@Override
            public void onClick(View v)
            {

                Intent myint = new Intent();

                myint.setClass(LengthMainActivity.this, MainActivity.class);
                startActivity(myint);


            }
        });

        clear = (Button)this.findViewById(R.id.Clear);
        clear.setOnClickListener(new OnClickListener()
        {@Override
            public void onClick(View v)
            {
                edtext1.setText("");
                edtext2.setText("");
                edtext3.setText("");
                edtext4.setText("");
                edtext5.setText("");
                edtext6.setText("");
                edtext7.setText("");
            }
        });
    }

    @Override
    public void onFocusChange(View arg0, boolean arg1) {
        // TODO Auto-generated method stub

    }

}
于 2013-07-11T04:19:49.323 回答
0
this is my xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollview1"
    android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:fillViewport="true"
    tools:context=".LengthMainActivity">


        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="14dp"
                android:layout_marginTop="20dp"
                android:text="@string/Please_provide_length" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="40dp"
                android:text="@string/Kilometers_To_Meters" />

            <EditText

                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:ems="10"
                android:inputType="number|numberDecimal"
                android:singleLine="true"
                android:focusable="true"
                android:focusableInTouchMode="true" />
           <requestFocus/>
            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:text="@string/Meters_To_Centimeters" />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:ems="10"
                android:inputType="numberDecimal|numberDecimal" 
                android:focusable="true"
                android:focusableInTouchMode="true"/>

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:text="@string/Kilometer_To_Mile" />

            <EditText
                android:id="@+id/editText3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:ems="10"
                android:inputType="numberDecimal|numberDecimal" 
                android:focusable="true"
                android:focusableInTouchMode="true"/>

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:text="@string/Yards_To_Meters" />

            <EditText
                android:id="@+id/editText4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:ems="10"
                android:inputType="numberDecimal|numberDecimal"
                android:focusable="true"
                android:focusableInTouchMode="true" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:text="@string/Feet_To_Centimeters" />

            <EditText
                android:id="@+id/editText5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:ems="10"
                android:inputType="numberDecimal|numberDecimal" 
                android:focusable="true"
                android:focusableInTouchMode="true"/>

            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:text="@string/Feet_To_Inches" />

            <EditText
                android:id="@+id/editText6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:ems="10"
                android:inputType="numberDecimal|numberDecimal" 
                android:focusable="true"
                android:focusableInTouchMode="true"/>

            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:text="@string/Centimeter_To_Inches" />

            <EditText
                android:id="@+id/editText7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:ems="10"
                android:inputType="numberDecimal|numberDecimal" 
                android:focusable="true"
                android:focusableInTouchMode="true"/>

            <Button
                android:id="@+id/button1"
                android:layout_width="292dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:text="@string/Convert" />

            <Button
                android:id="@+id/button2"
                android:layout_width="290dp"
                android:layout_height="wrap_content"
                android:text="@string/Back" />

            <Button
                android:id="@+id/Clear"
                android:layout_width="287dp"
                android:layout_height="wrap_content"
                android:text="@string/Clear" />
        </LinearLayout>


    </ScrollView>
    enter code here
于 2013-07-11T04:25:54.510 回答