AIDE Android App基础教程_第二十三课-提示对话框

第二十三课,本课中您将学习到如何显示一个反馈给用户的提示对话框 运行这个App。

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onButtonClick(View view)
    {
    }
}

我们已经新增一个对话框类作为对话框的容器

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onButtonClick(View view)
    {
    }
}

class MyDialogFragment extends DialogFragment
{
	
}

覆写创建对话框的“public Dialog onCreateDialog(Bundle savedInstanceState)”方法

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onButtonClick(View view)
    {
    }
}

class MyDialogFragment extends DialogFragment
{

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		return super.onCreateDialog(savedInstanceState);
	}
	
}

我们用“AlertDialog.Builder”创建一个提示对话框

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onButtonClick(View view)
    {
    	
    }
}

class MyDialogFragment extends DialogFragment
{
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		
		return builder.create();
	}
	
}

“create()”方法创建提示对话框,接下来我们来配置builder

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onButtonClick(View view)
    {
    	
    }
}

class MyDialogFragment extends DialogFragment
{
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		
		return builder.create();
	}
	
}

调用builder的“setTitle”方法并将“My Dialog”作为参数

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onButtonClick(View view)
    {
    	
    }
}

class MyDialogFragment extends DialogFragment
{
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("My Dialog");
		return builder.create();
	}
	
}

现在调用编译器的“setMessage”方法并将“This is my message”作为参数

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onButtonClick(View view)
    {
    	
    }
}

class MyDialogFragment extends DialogFragment
{
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("My Dialog");
		builder.setMessage("This is my message");
		return builder.create();
	}
	
}

在“onButtonClick”方法内新建“MyDialogFragment”实例

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onButtonClick(View view)
    {
    	MyDialogFragment dialog=new MyDialogFragment();
    }
}

class MyDialogFragment extends DialogFragment
{
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("My Dialog");
		builder.setMessage("This is my message");
		return builder.create();
	}
	
}

通过“getFragmentManager()”和“”mydialog””调用“show()”方法

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onButtonClick(View view)
    {
    	MyDialogFragment fragment=new MyDialogFragment();
		fragment.show(getFragmentManager(),"mydialog");
    }
}

class MyDialogFragment extends DialogFragment
{
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("My Dialog");
		builder.setMessage("This is my message");
		return builder.create();
	}
	
}

用“setPositiveButton()”添加“Ok”按钮

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.content.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onButtonClick(View view)
    {
    	MyDialogFragment f = new MyDialogFragment();
		f.show(getFragmentManager(), "mydialog");
    }
}

class MyDialogFragment extends DialogFragment
{
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("My Dialog");
		builder.setMessage("This is my message");
		builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface p1, int p2)
				{
					// TODO: Implement this method
				}
			});
		
		return builder.create();
	}
	
				}

再用“setNegativeButton()”添加“Cancel”按钮

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.content.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    public void onButtonClick(View view)
    {
    	MyDialogFragment f = new MyDialogFragment();
		f.show(getFragmentManager(), "mydialog");
    }
}

class MyDialogFragment extends DialogFragment
{
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("My Dialog");
		builder.setMessage("This is my message");
		builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface p1, int p2)
				{
					// TODO: Implement this method
				}
			});
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

				@Override
				public void onClick(DialogInterface p1, int p2)
				{
					// TODO: Implement this method
				}
			});
		return builder.create();
	}
	
}

 

© 版权声明
THE END
喜欢就支持以下吧
点赞2赞赏
分享
评论 抢沙发

请登录后发表评论