Dialog 和 Activity 之间传递数据
来源:互联网 发布:cam是什么软件 编辑:程序博客网 时间:2024/05/18 02:34
自定义Dialog 类
package com.cikuu.pigai.activity.dialog;import android.app.Activity;import android.os.Bundle;import android.support.v4.app.DialogFragment;import android.util.Log;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.EditText;import com.cikuu.pigai.R;/** * Created by Administrator on 2015/1/15. */public class ModifyInfoDialog extends DialogFragment { private Button btnOne; private Button btnTwo; private EditText mScoreEditText; public String mScore; private static int mPosition; ModifyInfoDialog f; public interface NoticeDialogListener { public void onDialogPositiveClick(DialogFragment dialog, String score, int position); public void onDialogNegativeClick(DialogFragment dialog); } // Use this instance of the interface to deliver action events NoticeDialogListener mListener; public ModifyInfoDialog() { // Empty constructor required for DialogFragment } public static ModifyInfoDialog newInstance(String score ,int position) { ModifyInfoDialog f = new ModifyInfoDialog(); // Supply score input as an argument. Bundle args = new Bundle(); args.putString("score", score); args.putInt("position", position); Log.d("score", score); f.setArguments(args); mPosition = f.getArguments().getInt("position",0); return f; } //Override the Fragment.onAttach() method to instantiate the NoticeDialogListener @Override public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (NoticeDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); View view = inflater.inflate(R.layout.dialog_input_score2, container); mScoreEditText = (EditText)view.findViewById(R.id.inputScoreEditText); btnOne = (Button) view.findViewById(R.id.btnOne); btnOne.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { mScore = mScoreEditText.getText().toString(); mListener.onDialogPositiveClick(ModifyInfoDialog.this, mScore, mPosition); } catch(NumberFormatException e) { return; } } }); btnTwo = (Button) view.findViewById(R.id.btnTwo); btnTwo.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { mListener.onDialogNegativeClick(ModifyInfoDialog.this); } }); return view; } @Override public void onResume() { getDialog().getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM); WindowManager.LayoutParams p = getDialog().getWindow().getAttributes(); p.width = WindowManager.LayoutParams.MATCH_PARENT; p.height = WindowManager.LayoutParams.WRAP_CONTENT; //TODO set the offset from the bottom p.y = 0; //TODO set the animation p.windowAnimations = R.style.DialogAnimation; getDialog().getWindow().setAttributes(p); super.onResume(); }}
布局文件 <span style="font-family: Arial, Helvetica, sans-serif;">dialog_input_score2</span>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/input_score" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="64dp" android:scaleType="center" android:text = "修改信息" android:textSize="20sp" android:gravity="center" android:textColor="#0000FF" android:textStyle="bold" android:background="#FFFFBB33" /> <EditText android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/inputScoreEditText" /> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btnOne" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/btnTwo" android:layout_toRightOf="@+id/view" android:text="确定" /> <View android:id="@+id/view" android:layout_width="0dp" android:layout_height="1dp" android:layout_centerHorizontal="true" /> <Button android:id="@+id/btnTwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/view" android:text="取消" /> </RelativeLayout></LinearLayout>
<pre name="code" class="java">activity中<span style="font-family: Arial, Helvetica, sans-serif;">调用</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">package com.cikuu.pigai.activity.student;import android.app.Dialog;import android.provider.ContactsContract;import android.support.v4.app.DialogFragment;import android.support.v4.app.FragmentManager;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import com.cikuu.pigai.R;import com.cikuu.pigai.activity.dialog.InputScoreDialog;import com.cikuu.pigai.activity.dialog.ModifyInfoDialog;import com.cikuu.pigai.businesslogic.Student;import com.cikuu.pigai.httprequest.VolleyRequest;import java.util.ArrayList;import java.util.HashMap;import java.util.List;public class StudentInformationActivity extends ActionBarActivity <span style="color:#3366ff;">implements ModifyInfoDialog.NoticeDialogListener </span>,VolleyRequest.UserInfoModifyCallback { private VolleyRequest mHttpRequest; private ListView mListView; private Student mStudent; ModifyInfoDialog mModifyInfoDialog; StudentInformationAdapter adapter; ArrayList<Information> listArray; TextView info_value; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_student_information); mStudent = Student.GetInstance(); mHttpRequest = new VolleyRequest(); mHttpRequest.mUserInfoModifyCallback =this; listArray = new ArrayList<>(); Information info1 = new Information("姓名", mStudent.GetDescription().mName); Information info2 = new Information("学校", mStudent.GetDescription().mSchool); Information info3 = new Information("学号", mStudent.GetDescription().mStudent_number); Information info4 = new Information("班级", mStudent.GetDescription().mClass); Information info5 = new Information("邮箱", mStudent.GetDescription().mEmail); Information info6 = new Information("性别", mStudent.GetDescription().mSex); Information info7 = new Information("院系", "暂无"); Information info8 = new Information("生日", "暂无"); Information info9 = new Information("手机", mStudent.GetDescription().mTel); listArray.add(info1); listArray.add(info2); listArray.add(info3); listArray.add(info4); listArray.add(info5); listArray.add(info6); listArray.add(info7); listArray.add(info8); listArray.add(info9); mListView = (ListView) findViewById(R.id.user_info); adapter = new StudentInformationAdapter(listArray); mListView.setAdapter(adapter); <span style="color:#3366ff;"> mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { FragmentManager fm = getSupportFragmentManager(); mModifyInfoDialog = ModifyInfoDialog.newInstance("60",position); mModifyInfoDialog .show(fm, "score"); } });</span> } @Override public void UserInfoModify() { Toast.makeText(this, "修改成功", Toast.LENGTH_LONG).show(); } @Override public void ErrorNetwork() { } <span style="color:#3366ff;"> @Override public void onDialogPositiveClick(DialogFragment dialog, String modify, int position) { listArray.get(position).value = modify; adapter.notifyDataSetChanged(); mHttpRequest.UserInfoModify(mStudent.GetDescription().mUid, listArray.get(0).value , listArray.get(1).value, listArray.get(2).value, listArray.get(3).value); mStudent.mStudentDescription.mName = listArray.get(0).value; mStudent.mStudentDescription.mSchool =listArray.get(1).value; mStudent.mStudentDescription.mStudent_number =listArray.get(2).value; mStudent.mStudentDescription.mClass=listArray.get(3).value; mModifyInfoDialog.dismiss(); } @Override public void onDialogNegativeClick(DialogFragment dialog) { mModifyInfoDialog.dismiss(); }</span> class Information { String name; String value; Information( String n, String v) { name = n; value = v; } } public class StudentInformationAdapter extends BaseAdapter { private ArrayList<Information> mListItems; public StudentInformationAdapter(ArrayList<Information> listItems) { mListItems = listItems; } @Override public int getCount() { return mListItems.size(); } @Override public Object getItem(int position) { return mListItems.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view; view = View.inflate(StudentInformationActivity.this,R.layout.student_info_listview,null) ; Information item = mListItems.get(position); TextView info = (TextView) view.findViewById( R.id.user_info); info_value = (TextView) view.findViewById( R.id.user_value); info.setText(item.name); info_value.setText(item.value); return view; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_student_information, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}</span></span>
<span style="white-space:pre"></span>
0 2
- Dialog 和 Activity 之间传递数据
- Activity之间跳转和传递数据
- intent和activity之间传递数据
- fragment和activity之间的数据传递
- Fragment和Activity之间的数据传递
- Activity和Fragment之间的数据传递
- Activity 和 Fragment 之间的数据传递
- Activity之间startActivityForResult传递和接受数据
- Fragment和Activity之间的数据传递
- Activity和fragment之间传递数据(一)
- Activity和fragment之间传递数据(二)
- Dialog向Activity传递数据
- Activity之间数据传递
- Activity之间传递数据
- Activity之间传递数据
- Activity 之间传递数据
- Activity之间传递数据
- Activity之间传递数据
- JS 判断输入字符串的长度(中文占用两个字节,英文占用一个字节)
- Eclipse或Myeclipse导出JAR包以及如何合并两个JAR
- JAVA 环境变量
- (UVA) The ? 1 ? 2 ? ... ? n = k problem
- 学习C++编程, 调试
- Dialog 和 Activity 之间传递数据
- ubuntu开启SSH服务
- UIView与CALayer的区别,很详细
- Qt程序开发问题归集(一)
- MySql时间的显示
- linux下socket连接下的心跳机制
- 新手学Android之在windows下搭建Android开发环境以及HelloWorld
- LeetCode: Multiply Strings
- projspider.com恢复对猪八戒,一品威客网,软件项目交易网的支持