简体   繁体   English

打开键盘时,Android TextViews是否清除?

[英]Android TextViews clearing when keyboard opens?

I'm new to android and have been playing around with a simple stock-take app and have run in to this issue: 我是android的新手,并且一直在使用一个简单的盘点应用程序,并且遇到了这个问题:

On my "set stock" screen the product list and current stock are grabbed from a database and displayed in 2 columns of TextViews which are generated programmatically. 在我的“设定库存”屏幕上,产品列表和当前库存都从数据库中获取,并显示在以编程方式生成的TextViews的2列中。

The onCreate method which grabs from the database and puts it in to the empty ListView in activity_set.xml: 从数据库中获取并将其放入activity_set.xml中的空ListView的onCreate方法:

public class SetActivity extends AppCompatActivity{
    private static final String TAG = "SetActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);

        ArrayList<Product> stock = Products.retrieveProducts(this);
        final ListView mListView = (ListView) findViewById(setList);
        ProductEditAdapter adapter = new ProductEditAdapter(this,R.layout.adapter_set_layout, stock);
        mListView.setAdapter(adapter);

    }

ProductEditAdapter class: ProductEditAdapter类:

public class ProductEditAdapter extends ArrayAdapter<Product> {
    private static final String TAG = "ProductEditAdapter";
    private Context mContext;
    int mResource;


    public ProductEditAdapter(Context context, int resource, ArrayList<Product> objects){
        super(context,resource,objects);
        mContext = context;
        mResource = resource;

    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String name = getItem(position).getName();
        int stock = getItem(position).getStock();
        Product product = new Product(name,stock);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(mResource, parent, false);
        TextView tvName = (TextView) convertView.findViewById(R.id.pie_name);
        TextView tvCurrentStock = (TextView) convertView.findViewById(R.id.current_stock);
        tvCurrentStock.setText(String.valueOf(stock));
        tvName.setText(name + ":");
        tvCurrentStock.setTag(name);
        return convertView;
    }
}

The adapter_set_layout.xml it is being created with: 使用以下命令创建的adapter_set_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/adapsetLayout"
    android:weightSum="100">

    <TextView
        android:gravity="center"
        android:textAlignment="center"
        android:text="TextView1"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:id="@+id/pie_name"
        android:layout_weight="20"
        android:textColor="#000000"
        android:textStyle="bold"
   />

    <TextView
        android:gravity="center"
        android:textAlignment="center"
        android:text="TextView1"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:id="@+id/current_stock"
        android:layout_weight="80"
        android:textColor="#000000"
        android:textStyle="bold"
        android:onClick="onClick"
        android:clickable="true"
        />
</LinearLayout>

When you click the stock of an item you're given a popup to enter a new number where you can then click "Ok" to update that TextView or "Cancel" to not. 当您单击某个项目的库存时,会弹出一个窗口以输入一个新数字,然后您可以单击“确定”以将TextView或“取消”更新为不。

There's then a submit button which parses through all the TextViews and updates the stock numbers of each product in the database to the new values. 然后有一个提交按钮,该按钮分析所有TextView,并将数据库中每种产品的库存编号更新为新值。

The changed TextView values are temporary until hitting submit, and will revert back to the values they're grabbed from if you leave the screen. 更改的TextView值是临时的,直到单击“提交”为止,并且如果您离开屏幕,则将还原为从中获取的值。

The onClick method in SetActivity: SetActivity中的onClick方法:

public void onClick(View v){
    if(v.getId()==R.id.submitButton) {
      confirmDialog();
    }
    else if(v.getId()==R.id.current_stock) {
        LinearLayout parent = (LinearLayout) v.getParent();
        final TextView pie_stock_view = (TextView) parent.findViewById(current_stock);
        final TextView pie_name_view = (TextView) parent.findViewById(pie_name);


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(pie_name_view.getText().toString());

        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_NUMBER);
        builder.setView(input);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                pie_stock_view.setText(input.getText().toString());
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();


    }
    else if(v.getId()==R.id.resetButton) {
        resetDialog();
    }

}

The app works perfectly in the emulator - when you click a stock number and the box pops up, you can just type with the desktop keyboard and click ok, updating as many values as you want before submitting. 该应用程序可在仿真器中完美运行-单击股票编号并弹出框时,您只需使用桌面键盘键入内容,然后单击确定,即可在提交前更新任意数量的值。

The issue on an actual device is when the box pops up and you click in to it, the on screen keyboard pops up and I guess that counts as leaving the activity because all the updated values are reset back to their generated values, any suggestions how I could go about preventing this?. 实际设备上的问题是,当该框弹出并单击它时,屏幕上的键盘会弹出,并且我认为这是离开活动的原因,因为所有更新的值都将重置为它们的生成值,任何建议如何我可以继续预防吗?

You should store the changed values in your array. 您应该将更改的值存储在数组中。 In order to that first use another function to capture clicks and move the definition of stock Array to outside of onCreate. 为了做到这一点,首先使用另一个函数来捕获点击并将股票数组的定义移到onCreate之外。 So you can change your code to this: 因此,您可以将代码更改为此:

public class SetActivity extends AppCompatActivity{
    private static final String TAG = "SetActivity";
    ArrayList<Product> stock = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        if(stock == null)
            stock = Products.retrieveProducts(this);
        final ListView mListView = (ListView) findViewById(setList);
        ProductEditAdapter adapter = new ProductEditAdapter(this,R.layout.adapter_set_layout, stock);
        mListView.setAdapter(adapter);


        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView parent, View v, int position, long id){
                if(v.getId()==R.id.submitButton) {
                    confirmDialog();
                }
                else if(v.getId()==R.id.current_stock) {
                    LinearLayout parent = (LinearLayout) v.getParent();
                    final TextView pie_stock_view = (TextView) parent.findViewById(current_stock);
                    final TextView pie_name_view = (TextView) parent.findViewById(pie_name);


                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle(pie_name_view.getText().toString());

                    final EditText input = new EditText(this);
                    input.setInputType(InputType.TYPE_CLASS_NUMBER);
                    builder.setView(input);

                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        pie_stock_view.setText(input.getText().toString());
                        getItem(position).setStock(input.getText().toString());
                        }
                    });
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                        }
                    });

                    builder.show();

                }
                else if(v.getId()==R.id.resetButton) {
                    resetDialog();
                }

            }
        });
    }
}

Another important thing is try using convertView. 另一个重要的事情是尝试使用convertView。 It'll prevent memory leak and improve application's performance. 它将防止内存泄漏并提高应用程序的性能。

 @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String name = getItem(position).getName();
        int stock = getItem(position).getStock();
        Product product = new Product(name,stock);
        if(convertView != null){
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);
        }
        TextView tvName = (TextView) convertView.findViewById(R.id.pie_name);
        TextView tvCurrentStock = (TextView) convertView.findViewById(R.id.current_stock);
        tvCurrentStock.setText(String.valueOf(stock));
        tvName.setText(name + ":");
        tvCurrentStock.setTag(name);
        return convertView;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM