在输入编辑文本时,Horizo​​ntalScrollView会自动向右移动(在列的末尾)

时间:2015-01-14 06:48:52

标签: android xml android-edittext horizontalscrollview

我的问题是,当键入ScrollView时,水平EditText中的滚动会自动移动到屏幕右侧(列的结尾)。这个问题是我无法看到我在编辑文本中输入的内容。

这是我的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:tag="dasd"
android:id="@+id/linearlayout1">


<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"

    android:id="@+id/horizontalScrollView" >


    <TableLayout android:id="@+id/table_grades" android:layout_height="wrap_content"      android:layout_width="wrap_content"   >


    </TableLayout>



</HorizontalScrollView>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:layout_gravity="center"
        android:id="@+id/btnGet" />

</LinearLayout>


</LinearLayout>

我的代码:

     public class MainActivity extends ActionBarActivity {


    List<String> activity = new ArrayList<String>(Arrays.asList("Long exam 1", "Long exam 2", "Long     exam 3", "asdasddadsasd", "asdasdasdas", "dasdsasdasds", "dasdsasdasds", "dasdsasdasds"));
    List<String> students = new ArrayList<String>(Arrays.asList("asd", "dd", "dd"));

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

        TableLayout table_grades = (TableLayout) findViewById(R.id.table_grades);
       final HorizontalScrollView s = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);


        TableRow trHeader = new TableRow(this);
        TableRow.LayoutParams llp = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,    TableRow.LayoutParams.FILL_PARENT);
        trHeader.setLayoutParams(llp);
        TextView studentName = new TextView(this);
        studentName.setBackgroundResource(R.drawable.cell_shape_header_studentname);
        studentName.setPadding(0, 5, 0, 0);
        studentName.setText("Student Name");
        studentName.setTextColor(Color.parseColor("#FFFFFFFF"));
        studentName.setTypeface(null, Typeface.BOLD);


        TextView[] headers = new TextView[activity.size()];
        for (int i = 0; i < activity.size(); i++) {
            headers[i] = new TextView(this);

            headers[i].setBackgroundResource(R.drawable.cell_shape_header_activities);
            headers[i].setPadding(0, 5, 0, 0);
            headers[i].setText(activity.get(i).toString());
            headers[i].setTextColor(Color.parseColor("#FFFFFFFF"));
            headers[i].setTypeface(null, Typeface.BOLD);



        }

        trHeader.addView(studentName);
        for (int i = 0; i < headers.length; i++) {
            trHeader.addView(headers[i]);
        }

        table_grades.addView(trHeader);

        TableRow[] studentRow = new TableRow[students.size()];
        TextView[] studentRecord;
        for (int i = 0; i < students.size(); i++) {
            studentRow[i] = new TableRow(this);
            studentRow[i].setLayoutParams(llp);
            studentRecord = new TextView[students.size()];

            studentRecord[i] = new TextView(this);
            studentRecord[i].setBackgroundResource(R.drawable.cell_shape);
            studentRecord[i].setPadding(0, 5, 0, 0);
            studentRecord[i].setLayoutParams(llp);
            studentRecord[i].setText(students.get(i).toString());
            studentRecord[i].setTextColor(Color.parseColor("#FFFFFFFF"));
            studentRow[i].addView(studentRecord[i]);

        }
        EditText ed;
        final List<EditText> studentEditTexts = new ArrayList<EditText>();


        for (int i = 0; i < studentRow.length; i++) {
            for (int j = 0; j < activity.size(); j++) {
                ed = new EditText(this);
                studentEditTexts.add(ed);
                ed.setBackgroundResource(R.drawable.cell_shape_edittexts);
                ed.setLayoutParams(llp);
                ed.setTypeface(null, Typeface.BOLD);
                ed.setGravity(Gravity.CENTER);
                ed.setFilters(new InputFilter[]{new InputFilter.LengthFilter(4)});
                ed.setInputType(InputType.TYPE_CLASS_NUMBER);
                ed.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        s.fullScroll(HorizontalScrollView.FOCUS_LEFT);
                    }
                });
                studentRow[i].addView(ed);
            }
        }

        for (int i = 0; i < studentRow.length; i++) {
            table_grades.addView(studentRow[i]);
        }


        Button bt = (Button) findViewById(R.id.btnGet);
        final String[] strings = new String[studentEditTexts.size()];

        for (int i = 0; i < studentEditTexts.size(); i++) {
            strings[i] = studentEditTexts.get(i).getText().toString();
        }
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /*Get inputs
                for (int i = 0; i < studentEditTexts.size(); i++) {
                    strings[i] = studentEditTexts.get(i).getText().toString();
                    Toast.makeText(MainActivity.this, strings[i] + " text length is: " +   strings[i].length(), Toast.LENGTH_SHORT).show();
                }
                */





            }
        });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

0 个答案:

没有答案