安卓在XML里设置的EditText,在activity写代码用EditText里输入的字符串 1C

安卓在XML里设置的EditText,在activity用EditText里输入的字符串,再和别的字符串比较,activity里获取输入的字符串的代码怎么实现呢?

4个回答

首先通过findviewbyId获取EditText,然后getText().toString()获取里面的字符串阿

u011249920
简一_hz 获取到的url和什么比较?
3 年多之前 回复
qq_30142939
qq_30142939 这是activity里的代码 就是想在这里获取URL地址通过字符串比较与数据里的字符串做字符串比较,能帮看一下吗?谢谢
3 年多之前 回复

public class MainActivity extends Activity {

private int total = 0;

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

    final EditText et_fileurl = (EditText) findViewById(R.id.fileurl);
    final Button btn_down = (Button) findViewById(R.id.btn_down);
    btn_down.setOnClickListener(new View.OnClickListener(){        //xiang ying shi jian

        public void onClick(View v){
            new Thread(new Runnable(){
                @Override
                public void run() {
                    try {
                    URL url =new URL(et_fileurl.getText().toString());    //资源

                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(5000);
                    conn.setRequestProperty("User-Agent","Mozilla/4.0 (compayible; MSIE8.0; Windows NT 5.1; Trient/4.0; .NET CLR 2.0.50727)");//伪装成浏览器
                    String str = conn.getResponseMessage();
                    int length = conn.getContentLength();//huo qu zheng ge wen jian de chang du
                    if(length <0 ){      //pan duan wen jian cun bu cun zai
                        Toast.makeText(MainActivity.this, "文件不存在",Toast.LENGTH_SHORT).show();
                        return;
                    }

                    File file = new File(Environment.getExternalStorageDirectory(),getFileName(et_fileurl.getText().toString()));//huo qu wen jian ming
                    RandomAccessFile randomFile = new RandomAccessFile(file, "rw");
                    randomFile.setLength(length);

                    int blocksize = length/3;
                    for(int i=0;i<3;i++){
                        int begin = i*blocksize;
                        int end = (i+1)*blocksize;
                        if(i==2){
                            end=length;
                        }
                        //chuang jian xin de xian cheng, xia zai wen jian
                        Thread t =new Thread(new DownloadRunnable(i, begin, end, file, url));
                        t.start();
                    }

                    }catch (MalformedURLException e){
                        Toast.makeText(MainActivity.this, "URL不正确",Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }).start();

        }
    });

}


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

private String getFileName(String url){
    int index = url.lastIndexOf("/");
    return url.substring(index);
}

class DownloadRunnable implements Runnable{

    private int begin;
    private int end;
    private File file;
    private URL url;
    private int id;

    public DownloadRunnable(int id, int begin, int end, File file, URL url){
        this.file=file;
        this.id=id;
        this.begin=begin;
        this.end=end;
        this.url=url;
    }
    @Override
    public void run() {
        // xia zai xing wei de bian xie
        try {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("User-Agent","Mozilla/4.0 (compayible; MSIE8.0; Windows NT 5.1; Trient/4.0; .NET CLR 2.0.50727)");
            conn.setRequestProperty("Range", "bytes=" + begin + "-" + end);

            InputStream is = conn.getInputStream();
            byte[] buf = new byte[1024 * 1024];
            RandomAccessFile randomFile = new RandomAccessFile(file, "rw");
            randomFile.seek(begin);
            int len = 0;
            while ((len = is.read(buf)) != -1 ){
                randomFile.write(buf,0,len);
                updateProgress(len);   // dui jin du geng xin
                Log.d("Download length: ","" + total);

            }

            is.close();
            randomFile.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

synchronized private void updateProgress(int add){
    total += add;
}

}

在activity里面
EditText 控件名;

初始化的时候
控件名= FindViewById(Resource.Id.xml里的id);
控件名.TextChanged += 控件名_TextChanged;

然后写Text改变函数
void 控件名_TextChanged(object sender, Android.Text.TextChangedEventArgs e)
{
String 一个str变量 = 控件名.Text;
}
然后怎么操作【一个str变量】就很简单了

楼上正解,写一个textChangedListener即可
mLoginPhoneEt.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() == 11) {
                mGetCodeBtn.setBackgroundResource(R.drawable.button9_orange_short);
                mGetCodeBtn.setEnabled(true);
            } else {
                mGetCodeBtn.setBackgroundResource(R.drawable.img_btn_noonclik);
                mGetCodeBtn.setEnabled(false);
            }
        }
    });
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问