今天在做一个分页的功能,其中有个页码跳转,是使用一个AlertDialog弹出一个页面布局。当输入页码点确定后,一直取不到EditText中的值,在苦思冥想和膜拜古哥度娘之后,终于解决,现在就和大家分享下:
先来看一段布局文件,文件名是page_goto.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到:"
android:textSize="28px"/>
<EditText
android:id="@+id/gotoPageIndex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:phoneNumber="true"
android:text="1"/>
<TextView
android:id="@+id/countPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28px"/>
</LinearLayout>
然后看一段错误的Java代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Map<String, String> map = new HashMap<String, String>();
map.put("clientName", clientName);
MenuUtil.operatePage(item, this, map);
switch (item.getItemId()) {
case R.id.pageGoTo:
Builder build = new AlertDialog.Builder(this);
LinearLayout ll = (LinearLayout)getLayoutInflater().inflate(R.layout.page_goto, null);
build.setView(ll);
TextView tv = (TextView)ll.findViewById(R.id.countPage);
tv.setText("/"+Global.countPage);
EditText et = (EditText)ll.findViewById(R.id.gotoPageIndex);
et.setText("22");//这里设置了22
build.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LinearLayout ll = (LinearLayout)getLayoutInflater().inflate(R.layout.page_goto, null);
EditTextet = (EditText)ll.findViewById(R.id.gotoPageIndex);//错误的地方
String page = et.getText().toString();//这里取到的还是1
Global.pageIndex = Integer.parseInt(page);
Intent intent = new Intent();
intent.putExtra("clientName", clientName);
intent.setClass(Customs.this, Customs.class);
Customs.this.startActivity(intent);
}
});
build.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
build.create().show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
这段代码是为了扩充Menu的功能。其中有一行我设置了et.setText("22"),但在【确定】按钮中怎么取值page的值都为配置文件中配置的“1”。
经过查询资料发现,错误的地方就在于【确定】按钮中那段
EditText et = (EditText)ll.findViewById(R.id.gotoPageIndex);
这里会取到的EditText,和我们设置的EditText是
两个对象,所以设置的22对【确定】中的EditText无效
下面是改正后的代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Map<String, String> map = new HashMap<String, String>();
map.put("clientName", clientName);
MenuUtil.operatePage(item, this, map);
switch (item.getItemId()) {
case R.id.pageGoTo:
Builder build = new AlertDialog.Builder(this);
LinearLayout ll = (LinearLayout)getLayoutInflater().inflate(R.layout.page_goto, null);
build.setView(ll);
TextView tv = (TextView)ll.findViewById(R.id.countPage);
tv.setText("/"+Global.countPage);
EditText et = (EditText)ll.findViewById(R.id.gotoPageIndex);
build.setPositiveButton("确定", new ClickListener(et) );//自己实现一个方法
build.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
build.create().show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
写一个内部类,实现OnClickListener接口,把得到的EditText对象传进去,就解决了操作两个对象的问题:
class ClickListener implements OnClickListener {
EditText et;
public ClickListener(EditText et){
this.et = et;
}
@Override
public void onClick(DialogInterface dialog, int which) {
String page = et.getText().toString();
Global.pageIndex = Integer.parseInt(page);
if(Global.pageIndex>Global.countPage||Global.pageIndex<1){
Toast.makeText(Customs.this, "跳转页码无效", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent();
intent.putExtra("clientName", clientName);
intent.setClass(Customs.this, Customs.class);
Customs.this.startActivity(intent);
}
}
这个问题说难,也许会被稍微有点经验的人都嗤之以鼻,但说容易,可能新手一时半会也解决不了(就像本人T T~ 其实是当时自己懒,不肯写个实现接口的内部类…… 不然估计会查出来的快些 )