2016-05-02 1 views
1

ci-dessous vous pouvez voir un script qui affiche les données d'une base de données SQLite dans une fenêtre contextuelle. Ma question est que comment puis-je mettre en place un bouton à l'intérieur de cette fenêtre pour accomplir certaines tâchesla mise en œuvre d'un bouton dans une fenêtre contextuelle pour Android

Voici le code:

package com.example.asus.sqlliteproject; 

import android.app.AlertDialog; 
import android.database.Cursor; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

    DataBaseHelper myDB; 
    EditText Name,LastName,Grades; 
    Button AddData,ViewData; 


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

     myDB = new DataBaseHelper(this); 
     Name = (EditText)findViewById(R.id.name); 
     LastName = (EditText)findViewById(R.id.lastName); 
     Grades = (EditText)findViewById(R.id.Grades); 
     AddData = (Button)findViewById(R.id.addDataButton); 
     ViewData = (Button)findViewById(R.id.view); 
     addData(); 

    } 
    public void addData() { 
     AddData.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       boolean inserted = myDB.insertData(Name.getText().toString(), 
         LastName.getText().toString(), 
         Grades.getText().toString()); 
       if (inserted) { 
        Toast.makeText(MainActivity.this, "Text Inserted!", Toast.LENGTH_SHORT).show(); 
       } else 
        Toast.makeText(MainActivity.this, "Unsuccessful!", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

    public void viewAll() { 
     ViewData.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Cursor res = myDB.getAllData(); 
       if (res.getCount() ==0){ 
        // show some message 
        showMessage("Error", "Nothing found"); 
        return; 
       } 
       StringBuffer buffer = new StringBuffer(); 
       while (res.moveToNext()) { 
        buffer.append("ID :"+ res.getString(0)+"\n"); 
        buffer.append("Name :"+ res.getString(1)+"\n"); 
        buffer.append("LastName :"+ res.getString(2)+"\n"); 
        buffer.append("Grade :"+ res.getString(3)+"\n\n"); 
       } 
       // show all data here 
       showMessage("Data",buffer.toString()); 

      } 
     }); 

    } 

    public void showMessage (String title, String message) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setCancelable(true); 
     builder.setTitle(title); 
     builder.setMessage(message); 
     builder.show(); 
    } 

} 

et voici une capture d'écran de l'application et ce que je veux mettre en œuvre:

Click here for image

Répondre

0

essayer,

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       //Your code 

      } 
     }); 

     builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
      //Your code 

      } 
     }); 
0
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
      context); 

     // set title 
     alertDialogBuilder.setTitle("Your Title"); 

     // set dialog message 
     alertDialogBuilder 
      .setMessage("Click yes to exit!") 
      .setCancelable(false) 
      .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, close 
        // current activity 
        MainActivity.this.finish(); 
       } 
       }) 
      .setNegativeButton("No",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, just close 
        // the dialog box and do nothing 
        dialog.cancel(); 
       } 
      }); 

      // create alert dialog 
      AlertDialog alertDialog = alertDialogBuilder.create(); 

      // show it 
      alertDialog.show(); 
     } 
    }); 
0

c'est simple.

public void showMessageDialog(String title , String Message) 
{ 

    AlertDialog dialog = new AlertDialog.Builder(Main3Activity.this) 
      .setTitle(title) 
      .setMessage(Message) 
      .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        //add your code here... 

       } 
      }) 

      .show(); 

} 
0

Si vous voulez doter votre bouton personnalisé - dans le coin gauche avec des propriétés personnalisées, vous devez utiliser "mise en page personnalisée" pour AlertBox.

Quelque chose comme ceci:

View tmLayout = View.inflate(ctx, R.layout.myalert, null); 
      TextView header= (TextView) tmLayout.findViewById(R.id.header); 
      Button btn= (Button) tmLayout.findViewById(R.id.btn); 

      btntzsetcur.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //callback 
       } 
      }); 
      header.setText("header); 
      btn.setText("btn"); 

      alerboxTimeZone = new AlertDialog.Builder(ctx).create(); 
      alerboxTimeZone.setView(tmLayout); 

Bien sûr, vous devez utiliser un fichier XML.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
      <TextView 
        android:id="@+id/header" 
        style="@style/whitetext" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/empty" /> 

      <LinearLayout 
       android:id="@+id/wrap" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" > 

      <Button 
       android:id="@+id/btn" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/empty" 
       android:textSize="20sp" /> 

      </LinearLayout> 


</LinearLayout> 
0

Vous pouvez créer une boîte de dialogue personnalisée avec une vue personnalisée.

Dialog dialog = new Dialog(_context); 
dialog.setContentView(YOUR_CUSTOM_VIEW); // put your xml file instead of YOUR_CUSTOM_VIEW 

boutons de vente ou tout ce que vous avez besoin dans votre fichier xml et à atteindre vos boutons comme ceci:

Button button = dialog.findViewById(BUTTON_ID); // R.id.YOUR_BUTTON 

et vous pouvez définir onClickListener et tout ce que vous voulez pour des trucs composant