简体   繁体   English

按下返回按钮清除editText的内容

[英]clear the contents of the editText on pressing back button

I want to clear the contents of editText. 我想清除editText的内容。 When back button is pressed while typing it should minimize the keyboard and take me back to the starting listview....Now doing so it directly exists the app.. And the keyboard appears on the screen once the app is started....instead i want it to appear when the user clicks on the editText..... 当在键入时按下后退按钮时,它应该最小化键盘并将我带回到开始的列表视图。...现在,它直接存在于该应用程序中。一旦启动该应用程序,键盘就会出现在屏幕上。相反,我希望它在用户单击editText时出现。

Here's my code: 这是我的代码:

package com.example.acer.aartisangrah;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class first extends AppCompatActivity {
EditText editText;
private ListView listView;

private String[] names = {"प्रार्थना", "सुखकर्ता दु:खहर्ता", "गजानना श्रीगणराया", "येई हो विठ्ठले", "लवथवती विक्राळा", "दुर्गे दुर्घट भारी", "आरती ज्ञानराजा",
        "हे भोळ्या शंकरा", "ओवाळू", "मंगेशा महारूद्रा", "आला गणराया", "शेंदुर लाल चढायो", "एकदंता", "कर्पूर गौरा", "हे गजवधना", "प्रथमेश्वरा", "जयदेवा हे  साईनाथा",
        "श्री सत्यनारायणाची आरती", "श्री महालक्ष्मीची आरती", "ॐ जय लक्ष्मी माता", "आरती संतोषी माता की", "धन्य धन्य हो प्रदक्षिणा", "सुखंकर्ता की दु:खहर्ता",
        "ॐ जय जगदीश", "हे गौरी गजानन", "घालीन लोटांगण"};
private int imageid[] = {R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji,
        R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji,
        R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji,
        R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji, R.drawable.ganeshaji};
int textlength = 0;
ArrayList<String> text_sort = new ArrayList<String>();

ArrayList<Integer> image_sort = new ArrayList<Integer>();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    listView = (ListView) findViewById(R.id.listView);
    editText = (EditText) findViewById(R.id.inputSearch);
    listView.setAdapter(new MyCustomAdapter(names, imageid));
    editText.addTextChangedListener(new TextWatcher()

    {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            textlength = editText.getText().length();
            text_sort.clear();
            image_sort.clear();
            for (int i = 0; i < names.length; i++) {
                if (textlength <= names[i].length()) {
                    if (editText.getText().toString().equalsIgnoreCase((String) names[i].subSequence(0, textlength))) {
                        text_sort.add(names[i]);
                        image_sort.add(imageid[i]);
                    }
                }
            }
            listView.setAdapter(new MyCustomAdapter(text_sort, image_sort));
        }

    });

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            TextView tv = (TextView) view.findViewById(R.id.textViewName);
            String strSelectedString = tv.getText().toString();
            for (int selectedPosition = 0; selectedPosition < names.length; selectedPosition++) {
                if (names[selectedPosition].equalsIgnoreCase(strSelectedString)) {
                    moveToNextActivity(selectedPosition);
                    break;
                }
            }

        }
    });
}


public void moveToNextActivity(int position) {
    if (position == 0) {
        Intent myIntent = new Intent(getApplicationContext(), akalp.class);
        startActivityForResult(myIntent, 0);
    }

    if (position == 1) {
        Intent myIntent = new Intent(getApplicationContext(), Sukharta.class);
        startActivityForResult(myIntent, 0);
    }

    if (position == 2) {
        Intent myIntent = new Intent(getApplicationContext(), gajananashriganraya.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 3) {
        Intent myIntent = new Intent(getApplicationContext(), yehihovittale.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 4) {
        Intent myIntent = new Intent(getApplicationContext(), lavathavthi.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 5) {
        Intent myIntent = new Intent(getApplicationContext(), durgedurgat.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 6) {
        Intent myIntent = new Intent(getApplicationContext(), nyanraja.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 7) {
        Intent myIntent = new Intent(getApplicationContext(), bolya.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 8) {
        Intent myIntent = new Intent(getApplicationContext(), vovalo.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 9) {
        Intent myIntent = new Intent(getApplicationContext(), mangesha.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 10) {
        Intent myIntent = new Intent(getApplicationContext(), alaganraya.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 11) {
        Intent myIntent = new Intent(getApplicationContext(), shendorlal.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 12) {
        Intent myIntent = new Intent(getApplicationContext(), ekdanta.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 13) {
        Intent myIntent = new Intent(getApplicationContext(), karpurgaura.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 14) {
        Intent myIntent = new Intent(getApplicationContext(), heygajavadana.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 15) {
        Intent myIntent = new Intent(getApplicationContext(), prathmeshvara.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 16) {
        Intent myIntent = new Intent(getApplicationContext(), sainatha.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 17) {
        Intent myIntent = new Intent(getApplicationContext(), satyanarayan.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 18) {
        Intent myIntent = new Intent(getApplicationContext(), jaidevijaidevi.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 19) {
        Intent myIntent = new Intent(getApplicationContext(), omjaimahalakshmi.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 20) {
        Intent myIntent = new Intent(getApplicationContext(), maitohaarti.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 21) {
        Intent myIntent = new Intent(getApplicationContext(), danyadanyaho.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 22) {
        Intent myIntent = new Intent(getApplicationContext(), sukhartakidukharta.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 23) {
        Intent myIntent = new Intent(getApplicationContext(), omjaijagdish.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 24) {
        Intent myIntent = new Intent(getApplicationContext(), heygaurigajanana.class);
        startActivityForResult(myIntent, 0);
    }
    if (position == 25) {
        Intent myIntent = new Intent(getApplicationContext(), ghalinlotangan.class);
        startActivityForResult(myIntent, 0);
    }
}

class MyCustomAdapter extends BaseAdapter {
    String[] data_text;
    int[] data_image;

    MyCustomAdapter() {

    }

    MyCustomAdapter(String[] names, int[] imageid) {
        data_text = names;
        data_image = imageid;
    }

    MyCustomAdapter(ArrayList<String> names, ArrayList<Integer> imageid) {
        data_text = new String[names.size()];
        data_image = new int[imageid.size()];
        for (int i = 0; i < names.size(); i++) {
            data_text[i] = names.get(i);
            data_image[i] = imageid.get(i);
        }
    }

    public int getCount() {
        return data_text.length;
    }

    public String getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row;
        row = inflater.inflate(R.layout.list_layout, parent, false);
        TextView textview = (TextView) row.findViewById(R.id.textViewName);
        ImageView imageview = (ImageView) row.findViewById(R.id.imageView);
        textview.setText(data_text[position]);
        imageview.setImageResource(data_image[position]);
        return (row);
    }
}
}

You will need to override your onBackPressed() method in your actiivty so that it does not finish 您将需要在活动中覆盖onBackPressed()方法,以使其无法完成

@Override
public void onBackPressed(){
    edittext.setText("");
    listView.setSelection(0);
    //or
    listView.smoothScrollToPosition(0);
}

The above will set the edittext to an empty string and then move your listview back to index 0 when you click the back button. 上面的代码将edittext设置为一个空字符串,然后在单击“后退”按钮时将列表视图移回索引0。

https://developer.android.com/reference/android/app/Activity.html#onBackPressed() https://developer.android.com/reference/android/app/Activity.html#onBackPressed()

EDIT 编辑

So in order to still close the application if you are at the top of the listview, you will need to add some logic to keep track of where you are in your listview. 因此,如果您仍处于列表视图的顶部,为了仍然关闭该应用程序,您将需要添加一些逻辑以跟踪您在列表视图中的位置。 The easiest way would be to create a boolean that you alter in a listview scroll listener. 最简单的方法是创建一个在列表视图滚动侦听器中更改的布尔值。

public class first extends AppCompatActivity {
EditText editText;
private ListView listView;
boolean atTop; --> add this variable here. This will monitor our top status.
...// rest of logic

Then we need to apply a scrollListener to your listview in your onCreate. 然后,我们需要在您的onCreate中将scrollListener应用于您的列表视图。

listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
                if(i == 0){
                    View v = myListView.getChildAt(0);
                    int offset = (v == null) ? 0 : v.getTop();
                    if (offset == 0) {
                        atTop = true;
                        return;
                    }
                }else{
                    atTop = false;
                }
            }
});

In the onscroll essentially we are checking if we are at the top or somewhere else. 本质上,在滚动中,我们正在检查是否位于顶部或其他位置。 We aren't checking for the bottom so this should work almost anywhere. 我们不会检查底部,因此它几乎可以在任何地方工作。 Then in your onBackPressed you would jsut do: 然后在onBackPressed中,您将执行jsut:

@Override
public void onBackPressed(){
     if(atTop){
          super.onBackPressed();
     }else {
          edittext.setText("");
          listView.setSelection(0);
          //or
          listView.smoothScrollToPosition(0);
      }
}

So now if we are at the top we want the onBackPressed to run the same as it normally would. 因此,现在如果我们位于顶部,我们希望onBackPressed能够正常运行。 If we aren't clear the edit text and scroll to the top. 如果我们不清除编辑文本,然后滚动到顶部。

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

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