您的位置:首页 > 移动开发 > Android开发

android开发之自定义dialog及dialog替换activity好处

2014-12-21 16:55 281 查看
根据我的项目经验,主要讲解两个方面知识点。

1)自定义全屏Dialog。

2)使用全屏Dialog替代Activity的好处。

一、如何自定义全屏Dialog

自定义Dialog是非常有必要的,如果直接使用系统Dialog的话,在不同的android手机里边显示的位置可能不同,比如小米,AlertDialog显示就在顶部。自定义Dialog 其实也比较简单,继承系统Dialog,在构造函数中添加样式。实现全屏。

public class MyDialog extends Dialog {

private Context mContext;

public MyDialog(Context context){
this(context,R.style.my_dialog);
}

public MyDialog(Context context, int theme) {
super(context, theme);
this.mContext = context;
}

}


样式代码如下:

<style name="my_dialog" parent="@android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/egame_fillbox</item>
</style>


egame_fillbox.xml文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<padding android:left="0dp" android:top="0dp" android:right="0dp"
android:bottom="0dp" />
</shape>


通过这个样式很容易实现了全屏。

接下来,就看在代码中如何使用了。

final MyDialog dialog = new MyDialog(this);
dialog.setContentView(R.layout.dialog);
Button bt = (Button) dialog.findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
dialog.show();


使用起来跟activity很像的。也可以通过setContentView添加布局,通过findViewById()查找空间。这样就实现了全屏Dialog了。

二、使用全屏Dialog替代Activity的好处

我们先假设一个场景。我们有一个A Activity界面,里边有ListView显示信息,点击某一个条目,跳转到B Activity界面,在B Activity界面中修改A Activity传递过来的对象。

修改完成以后,在A Activity onResultActivity里边拿到修改以后的对象。这时候就得讲修改以后的对象,更新到ListView的adapter中的list集合中去,但是,通过Intent传递

对象给B Activity,其实这时候对象已经发生了变化。就算A Activity拿到这个对象以后,遍历list集合时,是找不到的。当然就算是这样,我们也是有办法实现,但是还是不够直接,这时候如果直接使用全屏Dialog的话,

所以平时我们在activity传递对象时要特别留意。如果使用Dialog则不用担心这个问题。

先看一下ListActivity代码;

package com.example.dialogdemo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ListActivity extends Activity {

private ListView lv;
private ArrayList<Person> persons;
private ListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list);

lv = (ListView) this.findViewById(R.id.lv);

persons = new ArrayList<Person>();
for(int i = 0; i < 10; i++){
persons.add(new Person("aaa" + i, 20 + i));
if (i == 0){
System.out.println(persons.get(0).toString());
}
}
adapter = new ListAdapter(this, R.layout.item, persons);
lv.setAdapter(adapter);

lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Person p = persons.get(position);
Bundle bundle = new Bundle();
bundle.putSerializable("person", p);

Intent intent = new Intent(ListActivity.this, ItemActivity.class);
intent.putExtras(bundle);

System.out.println("ListActivity:" +  p.toString());

startActivityForResult(intent, 1);

}

});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
Person p =  (Person) data.getSerializableExtra("person");
System.out.println("ListActivity2:" +  p.toString());
super.onActivityResult(requestCode, resultCode, data);
}

}


给每个item注册单击事件。当单击某一个条目时,将当前的person对象传递给ItemActivity,通过这个界面修改person。修改以后返回给ListActivity界面。ItemActivity代码如下;

package com.example.dialogdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ItemActivity extends Activity implements OnClickListener {

private Person person;
private EditText etName;
private EditText etAge;
private Button btsave;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
etName = (EditText) this.findViewById(R.id.et_item_name);
etAge = (EditText) this.findViewById(R.id.et_item_age);
btsave = (Button) this.findViewById(R.id.bt_save);

Intent intent = getIntent();
person = (Person) intent.getSerializableExtra("person");
System.out.println("ItemActivity:" + person.toString());

etName.setText(person.getName());
etAge.setText(person.getAge() + "");

btsave.setOnClickListener(this);

}

@Override
public void onClick(View v) {
String strName = etName.getText().toString().trim();
String strAge = etAge.getText().toString().trim();
int age = Integer.parseInt(strAge);

person.setName(strName);
person.setAge(age);

Bundle bundle = new Bundle();
bundle.putSerializable("person", person);

Intent intent = new Intent();
intent.putExtras(bundle);

setResult(1, intent);
finish();

}

}


在两个不同界面中打印Person,打印结果如下:



从图片中可以看出,通过intent传递的对象,发生了改变。所以这时候通过ItemActivity修改的对象,不是最好的办法。如果使用Dialog呢?

final Person p = persons.get(position);

final MyDialog dialog = new MyDialog(ListActivity.this);
dialog.setContentView(R.layout.activity_item);
final EditText etName = (EditText) dialog.findViewById(R.id.et_item_name);
final EditText etAge = (EditText) dialog.findViewById(R.id.et_item_age);
Button bt = (Button) dialog.findViewById(R.id.bt_save);

etName.setText(p.getName());
etAge.setText(p.getAge() + "");
bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
String strName = etName.getText().toString().trim();
String strAge = etAge.getText().toString().trim();
int age = Integer.parseInt(strAge);

p.setName(strName);
p.setAge(age);
adapter.notifyDataSetChanged();
dialog.cancel();
}
});
dialog.show();


只要在setOnItemClickListener事件里边添加以上代码,就可以实现。而且代码直观。对象也不会发生改变。直接notifyDataSetChanged()即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: