2013-07-10 93 views
0

喜多的EditText所有我有一個小的疑難問題在滾動型7的EditText,當我把一些數據在第一的EditText和向下滾動,使按下按鈕(對於某些計算),我失去了我的數據和重點轉到其他edittext。任何人都可以幫助我。 非常感謝內滾動視圖功能的Android

+1

你應該包括你的代碼,看看有什麼是你的問題。 –

+0

看看這個答案http://stackoverflow.com/questions/8772714/edittext-loses-content-on-scroll-in-listview – Rahil2952

+0

謝謝你們的幫助,我發送代碼 – user2567490

回答

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 

    } 

} 
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