2015-05-26 3 views
1

Я создаю приложение для Android для своего школьного проекта.Мне нужно сделать кнопку, которая открывает пользовательское диалоговое окно

Я создал интерфейс нормальный.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity"> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView" 
     android:background="@drawable/mappic" 
     android:layout_above="@+id/zoomControls" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <ZoomControls 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/zoomControls" 
     android:layout_alignParentBottom="true" 
     android:layout_alignRight="@+id/imageView" 
     android:layout_alignEnd="@+id/imageView" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Obtenir le PCC" 
     android:id="@+id/button" 
     android:layout_alignBottom="@+id/zoomControls" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

теперь я хочу, чтобы это сделать, когда я нажимаю на кнопку: Obtenir ле PCC это диалоговое окно открывается:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:backgroundTintMode="multiply" 
    android:backgroundTint="#ff7518ff"> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@+id/Suivantbtn" 
     android:layout_alignLeft="@+id/Suivantbtn" 
     android:layout_alignStart="@+id/Suivantbtn" /> 

    <TextView 
     android:id="@+id/textdp" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="Taper le poit de départ:" 
     android:textColor="#ff111124" 
     android:layout_alignBottom="@+id/Suivantbtn" 
     android:theme="@style/AppTheme" 
     android:textSize="30dp" 
     android:layout_below="@+id/image" 
     android:layout_marginTop="50dp" 
     android:layout_marginBottom="50dp" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginRight="28dp" 
     android:layout_marginLeft="28dp" />/> 

    <Button 
     android:id="@+id/Suivantbtn" 
     android:layout_width="100px" 
     android:layout_height="wrap_content" 
     android:text="Suivant " 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:layout_above="@+id/Suivantbtn" 
     android:layout_marginBottom="56dp" 
     android:layout_alignLeft="@+id/textdp" 
     android:layout_alignStart="@+id/textdp" 
     android:layout_alignRight="@+id/textdp" 
     android:layout_alignEnd="@+id/textdp" 
     android:textAlignment="center" 
     android:backgroundTint="#ff9eadff" /> 

</RelativeLayout> 

и когда я нажимаю на Suivant ,, диалоговое окно идет и другой DialogBox появляется:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:backgroundTintMode="multiply" 
    android:backgroundTint="#ff7518ff"> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@+id/Suivantbtn2" 
     android:layout_alignLeft="@+id/Suivantbtn2" 
     android:layout_alignStart="@+id/Suivantbtn2" /> 

    <TextView 
     android:id="@+id/textar" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="Taper le poit d'arrivée:" 
     android:textColor="#ff111124" 
     android:layout_alignBottom="@+id/Suivantbtn2" 
     android:theme="@style/AppTheme" 
     android:textSize="30dp" 
     android:layout_below="@+id/image" 
     android:layout_marginTop="50dp" 
     android:layout_marginBottom="50dp" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginRight="28dp" 
     android:layout_marginLeft="28dp" />/> 

    <Button 
     android:id="@+id/Suivantbtn2" 
     android:layout_width="100px" 
     android:layout_height="wrap_content" 
     android:text="Suivant " 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:layout_above="@+id/Suivantbtn2" 
     android:layout_marginBottom="56dp" 
     android:layout_alignLeft="@+id/textar" 
     android:layout_alignStart="@+id/textar" 
     android:layout_alignRight="@+id/textar" 
     android:layout_alignEnd="@+id/textar" 
     android:textAlignment="center" 
     android:backgroundTint="#ff9eadff" /> 

</RelativeLayout> 

код Java:

package artofdev.org.admaps; 

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 


public class MainActivity extends Activity { 

    private ImageView imageView; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    Context context = getApplicationContext(); 

    final Button button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 
    @Override 
     public void onClick(View v) { 
      final Dialog dialog = new Dialog(context); 
      dialog.setContentView(R.layout.dialog); 
      Button button2 = (Button) dialog.findViewById(R.id.Suivantbtn); 
      button2.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        final Dialog dialog2 = new Dialog(context); 
        dialog2.setContentView(R.layout.dialog2); 
        Button button3 = (Button) 
    dialog2.findViewById(R.id.Suivantbtn2); 
        button3.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          dialog2.dismiss(); 
         } 
        }); 
        dialog.dismiss(); 
        dialog2.show(); 
       } 
      }); 
      dialog.show(); 
     } 
    }); 

    @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_main, 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); 
    } 
} 

И когда это появляется, и я печатаю и нажимаю дальше. Это диалоговое окно отклоняется, а в другом поле msg, которое я создам позже, отображается сообщение.

как это сделать?

+0

Вам нужно установить onClickListener на кнопку http://www.howtosolvenow.com/2013/11/android-button-setonclicklistener-example/ –

+1

Thanx братан ,, я на нем –

+0

первый узнать некоторые java и android programming - в Интернете есть много обучающих программ, а затем пишите вопросы, на которые НЕ ответили много раз или просто научились искать в Google .... –

ответ

0

Вот полностью рабочий код для вас:

Context context = getAplicationContext(); 
final Button button = (Button) findViewById(R.id.button); 
button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     final Dialog dialog = new Dialog(context); 
     dialog.setContentView(R.layout.your_dialog_layout); 
     Button button2 = (Button) dialog.findViewById(R.id.Suivantbtn); 
     button2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       final Dialog dialog2 = new Dialog(context); 
       dialog2.setContentView(R.layout.your_second_dialog_layout); 
       Button button3 = (Button) dialog2.findViewById(R.id.Suivantbtn2); 
       button3.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         dialog2.dismiss(); 
        } 
       }); 
       dialog.dismiss(); 
       dialog2.show(); 
      } 
     }); 
     dialog.show(); 
    } 
}); 

Надежда мой ответ помог!

+1

У меня есть что-то, что говорит: context это как ошибка –

+0

add this в начале вашего кода: «Контекстный контекст = getAplicationContext();' –

+1

теперь с: setOnClickListener все еще на ошибке –

-1

Вы должны добавить onClickListener к вашим кнопкам. Например (этот код для своего класса деятельности):

Button button = (Button) findViewById(R.id.button); 

button.setOnClickListener(new View.OnClickListener(){ 
    @Override 
    //On click function 
    public void onClick(View view) { 
     //do something - for example open the dialog box you want 
    } 
}); 
0
Button button = (Button) findViewById(R.id.button); 
button.setOnClickListener(new View.OnClickListener(){ 
    @Override 
    //On click function 
    public void onClick(View view) { 
     //do something - for example open the dialog box you want 
     final Dialog dialog = new Dialog(getActivity()); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.custom_alert_dialogue); 
     dialog.show(); 
    } 
}); 
Смежные вопросы