Android中自定义对话框小结

今天小结下在android中两个activity之间传递数据的知识点,其中还参考用了个模糊的效果。

1 主程序AndroDialog
  import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class AndroDialog extends Activity {
    /** Called when the activity is first created. */

    private Button startDialogBtn;
    private static final int MY_CUSTOM_DIALOG = 0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startDialogBtn = (Button)findViewById(R.id.startdialog);
        startDialogBtn.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
        startCustomDialog();

    }
        });
    }

        private void startCustomDialog() {
        Intent intent = new Intent(this,SearchDialog.class);
        startActivityForResult(intent, MY_CUSTOM_DIALOG);
        }
       
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode) {
        case (MY_CUSTOM_DIALOG) : {
        if (resultCode == Activity.RESULT_OK) {
//这里用日志去记录从自定义对话框中返回的结果
        Log.d("ANDRO_DIALOG","Coming back from the search dialog..");
       
//获得返回的数据
String searchQuery = data.getStringExtra(SearchDialog.SEARCH_QUERY_RESULT_FROM_DIALOG);
        Log.d("ANDRO_DIALOG", "Search query result: " + searchQuery);
        }
        break;
        }
        }

对应的mail.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button android:text="Show Custom Dialog"
    android:id="@+id/startdialog"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
</Button>
</LinearLayout>

2 弹出的第二个对话框
   SearchDialog.java
package com.liao;

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

public class SearchDialog extends Activity{

public static final String SEARCH_QUERY_RESULT_FROM_DIALOG = "SEARCH_DIALOG";
private Button searchBtn;
private Button cancelBtn;
private EditText searchEdit;


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
searchBtn = (Button)findViewById(R.id.search);
searchBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
returnSearchQuery();
}
});
cancelBtn = (Button)findViewById(R.id.cancel);
cancelBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
cancelDialog();
}
});
searchEdit = (EditText)findViewById(R.id.search_query);
searchEdit.setText("query");
getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

}
private void returnSearchQuery(){
Intent resultIntent = new Intent(this, SearchDialog.class);
resultIntent.putExtra(SEARCH_QUERY_RESULT_FROM_DIALOG, searchEdit.getText().toString());
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
private void cancelDialog(){
finish();
}
}
dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp">
    <EditText android:id="@+id/search_query"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    <LinearLayout android:orientation="horizontal"
    android:background="@android:drawable/bottom_bar" android:paddingLeft="4.0dip"
    android:paddingTop="5.0dip" android:paddingRight="4.0dip"
    android:paddingBottom="1.0dip" android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button android:id="@+id/search" android:layout_width="0.0dip"
        android:layout_height="fill_parent" android:text="Search"
        android:layout_weight="1.0" />
    <Button android:id="@+id/cancel" android:layout_width="0.0dip"
        android:layout_height="fill_parent" android:text="Cancel"
        android:layout_weight="1.0" />
</LinearLayout>
</LinearLayout>

配置文件:
  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.liao"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroDialog"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
           
           
        </activity>
   <activity android:name=".SearchDialog"
          android:label="SearchDialog"
          android:theme="@android:style/Theme.Dialog"/>
    </application>
        <uses-sdk android:minSdkVersion="8" />
</manifest>
        }

}

你可能感兴趣的:(xml,android,OS)