安卓在XML里设置的EditText,在activity用EditText里输入的字符串,再和别的字符串比较,activity里获取输入的字符串的代码怎么实现呢?
首先通过findviewbyId获取EditText,然后getText().toString()获取里面的字符串阿
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);
}
}
});