设置自定义对话框一旦进入活动 [英] Set custom dialog once into activity
问题描述
我有listactivity由多个行包含文字和两个按钮,其中一个打开无限画廊另一个打开的对话框每一行开启的活动,每个对话有不同的字符串,
我有20行,所以我添加对话框20倍,这是多余的,也是它的做工精细,但我认为还有更好的办法比我做什么,
任何帮助,让这将是AP preciated,谢谢
MyDay类:
公共类MyDay延伸活动{
最后上下文的背景下=这;
私人Button按钮;
TextView的TV1,TV2,TV3,TV4;
串日;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.Layou
tParams.FLAG_FULLSCREEN);
。getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
布尔customTitleSupported =
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
的setContentView(R.layout.day); 如果(customTitleSupported){
。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); } initializeTextViews(); }私人无效initializeTextViews(){
TV1 =(的TextView)findViewById(R.id.title_tv1);
tv1.setTypeface(FontFactory.getBFantezy(getBaseContext())); TV2 =(的TextView)findViewById(R.id.day_tv1);
tv2.setTypeface(FontFactory.getBFantezy(getBaseContext())); TV3 =(的TextView)findViewById(R.id.day_tv3);
tv3.setTypeface(FontFactory.getBFantezy(getBaseContext())); 天= getIntent()getStringExtra(奶酪)。 如果(day.equalsIgnoreCase(第一天)){
tv1.setText(第一天);
tv2.setText(Html.fromHtml(的getString(R.string.beginning)));
tv3.setText(Html.fromHtml(的getString(R.string.day1))); 按钮=(按钮)findViewById(R.id.city_button);
button.setOnClickListener(新OnClickListener(){
公共无效的onClick(查看为arg0){
//定制对话框
最后对话的对话=新的对话框(背景下,R.style.cust_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
//设置自定义对话框组件 - 文字,图片和按钮
TextView的文本=(TextView的)dialog.findViewById(R.id.dialog_text);
text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
text.setText(Html.fromHtml(的getString(R.string.torusim_places_1))); 按钮dialogButton =(按钮)dialog.findViewById(R.id.dialog_Button);
dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
//如果点击按钮,关闭对话框定制
dialogButton.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
dialog.dismiss();}
});
dialog.show(); }
}); } 否则如果(day.equalsIgnoreCase(第2天)){
tv1.setText(第二日);
tv2.setText(Html.fromHtml(的getString(R.string.beginning)));
tv3.setText(Html.fromHtml(的getString(R.string.day2))); 按钮=(按钮)findViewById(R.id.city_button);
button.setOnClickListener(新OnClickListener(){
公共无效的onClick(查看为arg0){
//定制对话框
最后对话的对话=新的对话框(背景下,R.style.cust_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog); TextView的文本=(TextView的)dialog.findViewById(R.id.dialog_text);
text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
text.setText(Html.fromHtml(的getString(R.string.torusim_places_2))); 按钮dialogButton =(按钮)dialog.findViewById(R.id.dialog_Button);
dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
//如果点击按钮,关闭对话框定制
dialogButton.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
dialog.dismiss(); }
});
dialog.show(); }
}); }
否则如果(day.equalsIgnoreCase(第三天)){
tv1.setText(第三天);
tv2.setText(Html.fromHtml(的getString(R.string.beginning)));
tv3.setText(Html.fromHtml(的getString(R.string.day3))); 按钮=(按钮)findViewById(R.id.city_button);
button.setOnClickListener(新OnClickListener(){
公共无效的onClick(查看为arg0){
//定制对话框
最后对话的对话=新的对话框(背景下,R.style.cust_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
TextView的文本=(TextView的)dialog.findViewById(R.id.dialog_text);
text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
text.setText(Html.fromHtml(的getString(R.string.torusim_places_3))); 按钮dialogButton =(按钮)dialog.findViewById(R.id.dialog_Button);
dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
//如果点击按钮,关闭对话框定制
ialogButton.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
dialog.dismiss(); }
});
dialog.show(); }
});
}
} //这种持续的重复,直到20天// 公共无效handleClick(视图v){ //创建一个意图,开始新的活动。 意向意图=新的Intent();
intent.setClass(这一点,DayGallery.class);
intent.putExtra(DAYNAME,日);
startActivity(意向); }
}
您有很多重复code的。我建议改变原意日表示为int额外的类型,并把你的文本资源与指数=天数阵列 - 1。
此外,它更容易阅读和维护code,如果你在你的活动类实现OnClickListener接口,而不是创建新的对象。
下面是重构你的code版本,更短和更清晰的:
公共类MyDay扩展活动实现View.OnClickListener {
私人语境mContext =这一点;
私人按钮mButton;
私人TextView的TV1,TV2,TV3,TV4;
私人诠释dayNumber;
私人对话支持mDialog;
//此字符串应该是在资源,像下面等
私人最终的String [] = tv1Resources {第一天,第二天,三日,...};
私人最终诠释[] = tv3Resources {R.string.day1,R.string.day2,R.string.day3,...};
私人最终诠释[] = dialogTextResources {R.string.torusim_places_1,R.string.torusim_places_2,R.string.torusim_places_3,...}; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
。getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
布尔customTitleSupported =
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
的setContentView(R.layout.day); 如果(customTitleSupported){
。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); } //注意,现在你应该把多余的整数,
//不串,这额外的只是数天-1,
//为第1天,这是0时,第2天,这是1等。
//你的情况,很可能这个数字在列表视图==项目位置
。dayNumber = getIntent()getIntExtra(奶酪,-1); previous活动
如果(dayNumber == -1){
完(); //这需要以某种方式,如果你得到额外的无效
返回;
} initializeTextViews();
} @覆盖
公共无效的onClick(视图v){
开关(v.getId()){
//其实不需要这个开关,因为你有
//只有一个在这项活动按钮,但我想表现,
//你怎么能在这里保持很多的onclick事件
案例R.id.city_button:
//定制对话框
//我们使用类领域避免最后一个对话框中的局部变量
支持mDialog =新的对话框(mContext,R.style.cust_dialog); mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.custom_dialog);
//设置自定义对话框组件 - 文字,图片和按钮
TextView的文本=(TextView的)mDialog.findViewById(R.id.dialog_text);
text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
text.setText(Html.fromHtml(的getString(dialogTextResources [dayNumber]))); 按钮dialogButton =(按钮)mDialog.findViewById(R.id.dialog_Button);
dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
//如果点击按钮,关闭对话框定制
dialogButton.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
如果(支持mDialog!= NULL)mDialog.dismiss(); //检查吵面板对话框空指针
}
});
mDialog.show();
打破;
}
} 私人无效initializeTextViews(){
TV1 =(的TextView)findViewById(R.id.title_tv1);
tv1.setTypeface(FontFactory.getBFantezy(getBaseContext())); TV2 =(的TextView)findViewById(R.id.day_tv1);
tv2.setTypeface(FontFactory.getBFantezy(getBaseContext()));
TV3 =(的TextView)findViewById(R.id.day_tv3);
tv3.setTypeface(FontFactory.getBFantezy(getBaseContext()));
mButton =(按钮)findViewById(R.id.city_button);
mButton.setOnClickListener(本); tv1.setText(tv1Resources [dayNumber]);
//由于code,这个资源是所有的日子一样
tv2.setText(Html.fromHtml(的getString(R.string.beginning)));
tv3.setText(Html.fromHtml(的getString(tv3Resources [dayNumber])));
} 公共无效handleClick(视图v){
//创建一个意图,开始新的活动。
意向意图=新的Intent();
intent.setClass(这一点,DayGallery.class);
intent.putExtra(DAYNAME,日);
startActivity(意向);
}
}
希望这有助于。
I have listactivity consist of multiple row each row open activity which contain text and two button one of them open infinite gallery the other one open dialog ,and each dialog had different string ,
i have 20 row so i add dialog 20 times which is redundant , also its work fine but i think there is better approach than what i did ,
any help to get it will be appreciated , thanks
MyDay class:
public class MyDay extends Activity {
final Context context = this;
private Button button;
TextView tv1,tv2,tv3,tv4;
String day;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.Layou
tParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Boolean customTitleSupported =
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.day);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); }
initializeTextViews(); }
private void initializeTextViews() {
tv1=(TextView)findViewById(R.id.title_tv1);
tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv2=(TextView)findViewById(R.id.day_tv1);
tv2.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv3=(TextView)findViewById(R.id.day_tv3);
tv3.setTypeface(FontFactory.getBFantezy(getBaseContext()));
day=getIntent().getStringExtra("cheese");
if(day.equalsIgnoreCase("Day1")){
tv1.setText("First Day");
tv2.setText(Html.fromHtml(getString(R.string.beginning)));
tv3.setText(Html.fromHtml(getString(R.string.day1)));
button = (Button) findViewById(R.id.city_button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context,R.style.cust_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.dialog_text);
text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
text.setText(Html.fromHtml(getString(R.string.torusim_places_1)));
Button dialogButton = (Button) dialog.findViewById(R.id.dialog_Button);
dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();}
});
dialog.show(); }
}); }
else if(day.equalsIgnoreCase("Day2")){
tv1.setText("Second Day");
tv2.setText(Html.fromHtml(getString(R.string.beginning)));
tv3.setText(Html.fromHtml(getString(R.string.day2)));
button = (Button) findViewById(R.id.city_button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context,R.style.cust_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
TextView text = (TextView) dialog.findViewById(R.id.dialog_text);
text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
text.setText(Html.fromHtml(getString(R.string.torusim_places_2)));
Button dialogButton = (Button) dialog.findViewById(R.id.dialog_Button);
dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss(); }
});
dialog.show(); }
}); }
else if(day.equalsIgnoreCase("Day3")){
tv1.setText("Third Day");
tv2.setText(Html.fromHtml(getString(R.string.beginning)));
tv3.setText(Html.fromHtml(getString(R.string.day3)));
button = (Button) findViewById(R.id.city_button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context,R.style.cust_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
TextView text = (TextView) dialog.findViewById(R.id.dialog_text);
text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
text.setText(Html.fromHtml(getString(R.string.torusim_places_3)));
Button dialogButton = (Button) dialog.findViewById(R.id.dialog_Button);
dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
// if button is clicked, close the custom dialog
ialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss(); }
});
dialog.show(); }
});
}
}
// this continuing repeated till day 20 //
public void handleClick(View v){
//Create an intent to start the new activity.
Intent intent = new Intent();
intent.setClass(this,DayGallery.class);
intent.putExtra("dayname",day);
startActivity(intent);
}
}
You have a lot of duplicating code. I suggest change intent day indicating extra type to int, and put your text resources into arrays with index = day number - 1.
Also it is easier to read and maintain code if you implement onClickListener interface in your activity class instead of creating new object.
Here is refactored version of your code, much shorter and clearer:
public class MyDay extends Activity implements View.OnClickListener {
private Context mContext = this;
private Button mButton;
private TextView tv1, tv2, tv3, tv4;
private int dayNumber;
private Dialog mDialog;
// this string should be in resources, like other below
private final String[] tv1Resources = {"First day", "Second day", "Third day" , ...};
private final int[] tv3Resources = {R.string.day1, R.string.day2, R.string.day3 , ...};
private final int[] dialogTextResources = {R.string.torusim_places_1, R.string.torusim_places_2, R.string.torusim_places_3 , ...};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Boolean customTitleSupported =
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.day);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); }
// note, now you should put integer extra,
//not String, this extra is just day number -1,
//for day1 this is 0, for day2 this is 1 etc.
//In your case, probably this number == item position in listview of
dayNumber = getIntent().getIntExtra("cheese", -1); previous activity
if (dayNumber == -1){
finish(); //this needed if somehow you get invalid extra
return;
}
initializeTextViews();
}
@Override
public void onClick(View v) {
switch (v.getId()){
//actually this switch isn't needed because you have
//only one button in this activity, but I wanted to show,
// how you can maintain many onClick events here
case R.id.city_button:
// custom dialog
// we use class field for avoid making final dialog local variable
mDialog = new Dialog(mContext, R.style.cust_dialog);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.custom_dialog);
// set the custom dialog components - text, image and button
TextView text = (TextView) mDialog.findViewById(R.id.dialog_text);
text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
text.setText(Html.fromHtml(getString(dialogTextResources[dayNumber])));
Button dialogButton = (Button) mDialog.findViewById(R.id.dialog_Button);
dialogButton.setTypeface(FontFactory.getBFantezy(getBaseContext()));
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mDialog != null) mDialog.dismiss(); // check for aviod dialog null pointer
}
});
mDialog.show();
break;
}
}
private void initializeTextViews() {
tv1 = (TextView) findViewById(R.id.title_tv1);
tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv2 = (TextView) findViewById(R.id.day_tv1);
tv2.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv3 = (TextView) findViewById(R.id.day_tv3);
tv3.setTypeface(FontFactory.getBFantezy(getBaseContext()));
mButton = (Button) findViewById(R.id.city_button);
mButton.setOnClickListener(this);
tv1.setText(tv1Resources[dayNumber]);
// due to code, this resource is the same for all days
tv2.setText(Html.fromHtml(getString(R.string.beginning)));
tv3.setText(Html.fromHtml(getString(tv3Resources[dayNumber])));
}
public void handleClick(View v) {
// Create an intent to start the new activity.
Intent intent = new Intent();
intent.setClass(this, DayGallery.class);
intent.putExtra("dayname", day);
startActivity(intent);
}
}
Hope this helps.
这篇关于设置自定义对话框一旦进入活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!